Go Back   RunUO - Ultima Online Emulation > Developer's Corner > Programming > C#

C# C# Discussion

Reply
 
Thread Tools Display Modes
Old 11-04-2006, 07:04 AM   #1 (permalink)
Forum Expert
 
Soteric's Avatar
 
Join Date: Aug 2006
Location: Russia, Rostov-on-Don
Posts: 458
Send a message via ICQ to Soteric
Default DateTime format converting

Hello. I have a DateTime in this format "MM-dd-yyyy HH:mm:ss". What I need is to make it like this "yyyy-MM-dd HH:mm:ss". According to manual I'm trying first to make my datetime to string and then, using ParseExact method, convert it back to DateTime. So it looks like:
Code:
DateTime dt = another_datetime_that_was_assigned_before;
string format = "yyyy-MM-dd HH:mm:ss";
string dt_string = dt.ToString(format);
DateTime converted_dt = DateTime.ParseExact(dt_string, format, null); // there could be a CultureInfo instead of "null" but I can't figure out how it affects and if I need it at all
As a result I get:
dt_string = "2006-11-04 15:00:00" // That's actually what I need
converted_dt = "04-11-2006 15:00:00" // Like my "another_datetime_ ... "

I've also tried to use any cultures instead of null (like "en-US", "ru-RU") but failed. Now I'm completely lost and can't get where my mistake is. Thanks for your help.
Soteric is offline   Reply With Quote
Old 11-04-2006, 07:16 AM   #2 (permalink)
Forum Expert
 
arul's Avatar
 
Join Date: Jan 2005
Location: Hiding in your room.
Age: 21
Posts: 1,272
Send a message via MSN to arul
Default

You do format only the input, not the output.

Try this:
Code:
 Console.WriteLine( dt.ToString( "yyyy-MM-dd HH:mm:ss" ) );
__________________
arul is offline   Reply With Quote
Old 11-04-2006, 07:37 AM   #3 (permalink)
Forum Expert
 
Soteric's Avatar
 
Join Date: Aug 2006
Location: Russia, Rostov-on-Don
Posts: 458
Send a message via ICQ to Soteric
Default

After converting it will be sent to SQL database, so I need it to be DateTime, not string. Output it on console isn't my aim.
Soteric is offline   Reply With Quote
Old 11-04-2006, 07:43 AM   #4 (permalink)
Forum Expert
 
Join Date: Jul 2005
Location: Istanbul/Turkey
Age: 27
Posts: 425
Default

You cant have DateTime in different formats.

Send it in String format (yyyy-MM-dd...) to SQL and it will do the rest for you..
__________________
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it."
noobie is offline   Reply With Quote
Old 11-04-2006, 07:54 AM   #5 (permalink)
Forum Expert
 
Soteric's Avatar
 
Join Date: Aug 2006
Location: Russia, Rostov-on-Don
Posts: 458
Send a message via ICQ to Soteric
Default

That works, thanks a lot I didn't know SQL would do this part of job for me )
Soteric is offline   Reply With Quote
Old 11-04-2006, 08:02 AM   #6 (permalink)
Forum Expert
 
arul's Avatar
 
Join Date: Jan 2005
Location: Hiding in your room.
Age: 21
Posts: 1,272
Send a message via MSN to arul
Default

Quote:
Originally Posted by Soteric
After converting it will be sent to SQL database, so I need it to be DateTime, not string. Output it on console isn't my aim.
Well, DateTime does not work the way you think.

In fact, DateTime is stored as a number ( meaning the number of ticks passed since the beginning of the epoch ), the input format is used only to calculate how many ticks to add.

The output to the console was only an example, how to produce a formatted string.
__________________
arul is offline   Reply With Quote
Old 11-04-2006, 08:42 AM   #7 (permalink)
Forum Expert
 
Soteric's Avatar
 
Join Date: Aug 2006
Location: Russia, Rostov-on-Don
Posts: 458
Send a message via ICQ to Soteric
Default

You are right. I thought DateTime worked completely another way. Thank you for explanation.
Soteric is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off



Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 RC5