Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

date conversion in visual C# 2

Status
Not open for further replies.

mcklsn

Programmer
Mar 16, 2003
40
US
Microsoft apparently stores dates as an integer number of days since Jan. 1, 1900. At least the application I'm interfacing with does. I need to find some C# or .net classes which will handle Microsoft format dates and figure what date is represented and create a string containing a human-readable formated date. Classes that go the other way, converting a human-readable date in the proper format into an integer number of days since 1900/01/01, would also be needed. The needed human-readable format is yyyy/mm/dd. Does anyone know a set of classes which are available and downloadable? Your help would be greatly appreciated. I needed this for use in Java, and received some custom written routines that worked perfectly. I am hoping to receive some help again. The date classes available seem unintelligable to me. I've been searching MSDN, but haven't found anything that I could use (or understand).
Thanks in advance for any help. It would be greatly appreciated.
 
You can use the System.DateTime classes:

To get from date to a double representing the date:

double dateNumeric;
System.DateTime dateIn = new DateTime(2003,10,3);
dateNumeric = dateIn.ToOADate();
// this obviously gets you the double representing the
// current date. You'll need to change this to
// pass in your particular date time you want to convert

To get from a double representing the date to a date time:

double daysNumeric = 31000;
System.DateTime date
= System.DateTime.FromOADate(daysNumeric);
// obviously change the daysNumeric field to your input
// numeric. You can then work with the datetime variable
// to get your desired format ie:
string dateFormatted = date.ToString("yyyy/MM/dd");

Also, I believe day 1 is 31/12/1899...

Hope this helps.
 
mcklsn -
Don't forget that the ToString method can also take a CultureInfo object that will provide date formatting based on the culture. So if you want US-style dates, create a CultureInfo object based on "en-US". If you want Austrian-style dates, pass "de-AT" to the CultureInfo constructor.

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Thanks, guys. This has been a great help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top