convert from one timeformat to another

Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jul 2009
Posts: 14
Reputation: tig2810 is an unknown quantity at this point 
Solved Threads: 0
tig2810 tig2810 is offline Offline
Newbie Poster

convert from one timeformat to another

 
0
  #1
Sep 12th, 2009
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.

  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. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,038
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 303
ddanbe's Avatar
ddanbe ddanbe is online now Online
Postaholic

Re: convert from one timeformat to another

 
0
  #2
Sep 13th, 2009
Use a DateTime to do it.
As an example look at this.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,721
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 501
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: convert from one timeformat to another

 
1
  #3
Sep 13th, 2009
Use DateTime.TryParseExact

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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 14
Reputation: tig2810 is an unknown quantity at this point 
Solved Threads: 0
tig2810 tig2810 is offline Offline
Newbie Poster

Re: convert from one timeformat to another

 
0
  #4
Sep 13th, 2009
DateTime.TryParseExact is exactly what I need. Works perfectly :-)
Reply With Quote Quick reply to this message  
Reply

Tags
format, time

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for format, time
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC