943,725 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 2190
  • C# RSS
Sep 12th, 2009
0

convert from one timeformat to another

Expand Post »
Hi all

I want to create a method that converts from one time or date format (as a string) to another by specifying the inputformat and the outputformat. I can do the output as below but I'm struggling on how to specify what the input format is. Can anyone help?
thanks.

C# Syntax (Toggle Plain Text)
  1.  
  2. //I'm halfway there.
  3. string date = FormatDateTime(myDate, "yyyyMMdd"));
  4. string time = FormatDateTime(myTime, "HHmm"));
  5.  
  6.  
  7. private string FormatDateTime(string dateortime, string outputformat)
  8. {
  9. string ret = "";
  10. if (dateortime == "")
  11. return "";
  12. try
  13. {
  14. ret = System.Convert.ToDateTime(dateortime).ToString(outputformat);
  15. }
  16. catch
  17. {
  18. }
  19. return ret;
  20. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tig2810 is offline Offline
20 posts
since Jul 2009
Sep 13th, 2009
0

Re: convert from one timeformat to another

Use a DateTime to do it.
As an example look at this.
Reputation Points: 2035
Solved Threads: 644
Senior Poster
ddanbe is offline Offline
3,736 posts
since Oct 2008
Sep 13th, 2009
1

Re: convert from one timeformat to another

Use DateTime.TryParseExact

Quote ...
SUMMARY:
Converts the specified string representation of a date and time to its DateTime equivalent using the specified format, culture-specific format information, and style. The format of the string representation must match the specified format exactly. The method returns a value that indicates whether the conversion succeeded.
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
Sep 13th, 2009
0

Re: convert from one timeformat to another

DateTime.TryParseExact is exactly what I need. Works perfectly :-)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tig2810 is offline Offline
20 posts
since Jul 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Hello, need help with dates and select statement
Next Thread in C# Forum Timeline: data string to array





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC