Answered by:
DateTime conversion

Question
-
Hey all, I posted a thread yesterday but I think I could have put it a lot simpler!
Would anyone be able to help me in converting a string like this:
"08/08/2007 09:48:02.265"
Into a Universal "u" time DateTime format?
I've tried many different variations of code but can't seem to make the conversion without it automatically being changed back to en-GB!
Thanks for reading, any suggestions will be greatly appreciated
Rob
Friday, September 7, 2007 8:49 AM
Answers
-
covertx wrote: Hey all, I posted a thread yesterday but I think I could have put it a lot simpler!
Would anyone be able to help me in converting a string like this:
"08/08/2007 09:48:02.265"
Into a Universal "u" time DateTime format?
I've tried many different variations of code but can't seem to make the conversion without it automatically being changed back to en-GB!
Thanks for reading, any suggestions will be greatly appreciated
Rob
Hi covertx,
As far as I know, if you would like to parse a date/time string and convert it to a UTC DateTime, use the DateTimeStyles enumeration AdjustToUniversal value with either the DateTime.Parse or DateTime.ParseExact method.
Some sample codes as follows for your reference:
string strDate = "08/08/2007 09:48:02.265";
DateTime dt0 = DateTime.Parse(strDate, null, DateTimeStyles.AdjustToUniversal);You can try to check out this document about "Formatting Date and Time for a Specific Culture" for details - http://msdn2.microsoft.com/en-us/library/5hh873ya(vs.80).aspx
Hope this helps,
Regards,
Citizens on the earth
Thursday, September 13, 2007 8:25 AM
All replies
-
DateTime DC ;
DC = DateTime.Parse("08/08/2007 09:48:02.265");
MessageBox.Show(DC.ToUniversalTime().ToString());
Friday, September 7, 2007 9:40 AM -
Hey, thanks for your input!
The output displayed in the message box is exactly the same as the string I parsed the date from... The universal DateTime conversion has had no affect!
Friday, September 7, 2007 10:24 AM -
The code posted earlier worked fine for me. Do you know what your UTC offset is? You might try this code to find out:
Code SnippetTimeZone localZone = TimeZone.CurrentTimeZone;
DateTime baseUTC = new DateTime(2000, 1, 1);
DateTime localTime = localZone.ToLocalTime(baseUTC);
MessageBox.Show(localZone.GetUtcOffset(localTime).ToString());
I (more or less) copied that from http://msdn2.microsoft.com/en-us/library/system.timezone.getutcoffset.aspx
My UTC offset is -6.00 (and this will change depending on what time zone you're in), so when I try:
Code SnippetDC =
DateTime.Parse("08/08/2007 09:48:02.265"); MessageBox.Show(DC.ToUniversalTime().ToString());I get the following displayed:
"8/8/2007 2:48:02 PM"
That's actually a difference of five hours, but you have to take daylight savings into account, too. Hope this helps.
Friday, September 7, 2007 1:46 PM -
Hey, thanks for your input!
I'm trying to get a universal date format not local date format though... Like this: "20007/08/08 . . ."...
Here's the code (along with previous attempts, none of which produced the results I required)...
Code Snippetforeach
(string daystr in e.Days){
string newDateTime = parsedDateTime.ToString("u");
/*1*///DateTime date = DateTime.Parse(newDateTime, CultureInfo.InvariantCulture);//, DateTimeFormatInfo.InvariantInfo);
/*2*///DateTime date = DateTime.ParseExact(daystr, "u", CultureInfo.CurrentCulture, DateTimeStyles.AdjustToUniversal); /*3*///DateTime date = DateTime.ParseExact(newDateTime, "yyyy-MM-ddTHH:mm:ss.fff", CultureInfo.InvariantCulture); /*4*///DateTime date = DateTime.Parse(newDateTime, CultureInfo.InvariantCulture, DateTimeStyles.None);if (!dateCollection.Contains(date))
{
dateCollection.Add(date);
}
}
Many thanks for sticking with me in reading this thread again!
Rob
Friday, September 7, 2007 2:02 PM -
Please forgive the probably AWFUL use of syntax for the numbered attempts!
Rob
Friday, September 7, 2007 2:16 PM -
DateTime localDate =
DateTime.ParseExact("08/08/2007 09:48:02.265", "MM/dd/yyyy HH:mm
s.fff", CultureInfo.InvariantCulture);
DateTime universalDate = localDate.ToUniversalTime();
string universalDateString = universalDate.ToString("yyyy-MM-ddTHH:mm
s.fff");
PS: Don't ever use DateTime.Parse
Friday, September 7, 2007 2:40 PM -
boban.s wrote: DateTime localDate =
DateTime.ParseExact("08/08/2007 09:48:02.265", "MM/dd/yyyy HH:mms.fff", CultureInfo.InvariantCulture);
DateTime universalDate = localDate.ToUniversalTime();
string universalDateString = universalDate.ToString("yyyy-MM-ddTHH:mms.fff");
PS: Don't ever use DateTime.Parse
why shouldn't we use DateTime.Parse?
Friday, September 7, 2007 3:36 PM -
Personally, i choose NOT to use DateTime.Parse because it's not explicit enough (it depends on the current thread's CultureInfo)...Friday, September 7, 2007 5:49 PM
-
Ryan Alford wrote: why shouldn't we use DateTime.Parse?
If you know how ParseExact work, and if you search my posts about ParseExact, you will know why. Parse method should not be used for any type, but especially not for DateTime.
Friday, September 7, 2007 9:06 PM -
cool. I will do some more research. I have seen a couple of guys that I work with use it, but I have never used it myself.Saturday, September 8, 2007 2:57 AM
-
Try this:
public static string UtcString2Universal(string utc) {
DateTime dt = DateTime.SpecifyKind(DateTime.Parse(utc), DateTimeKind.Utc);
return dt.ToString("u");
}
UtcString2Universal("08/08/2007 09:48:02.265")
produces: "2007-08-08 09:48:02Z"Monday, September 10, 2007 8:14 AM -
Many thanks for all your help everyone... After using your helpful suggestions (all with reasonable similarity to my initial attempts) the DateTime format I was after, is still out of reach!
Many thanks for your time and help!
Rob
Monday, September 10, 2007 8:24 AM -
Huh? Why don't you tell us what the output string should look like. It seems you have something else in mind than the "u" format.Monday, September 10, 2007 8:33 AM
-
Sorry for the delayed response!
I don't wish to get a string returned actually...
I've previously made an easy conversion from a local DateTime, to a Universal date time STIRNG... (So I have a string in the following format):
2007/10/08. . .
I'd now like to convert that from a string to a date time (but preserve the universal format)... I've tried many different approaches but none have been successful... I'm starting to wonder if I'm using a computer setting that's not allowing me to make this conversion from string to DateTime...
Thanks for you continued support!
Rob
Monday, September 10, 2007 2:27 PM -
You already get good answers on several occasions. Problem is that you probably don't know what is correct answer or what are you expecting to get. After request for better description of the problem, you again give a confusing description of the problem.
How visual studio debugger shows you the value of datetime variable is completely irrelevant. What you see is value.ToString() and you will get string representation of datetime value in format of current thread culture info. Format of date string is relevant when you use it somewhere, like writing on some label, in file, or somewhere else. But then you can format the date string anyway you want. If you use the datetime value as datetime, then what are you seeing in debuger is completely unimportant.
Monday, September 10, 2007 3:15 PM -
covertx wrote: Hey all, I posted a thread yesterday but I think I could have put it a lot simpler!
Would anyone be able to help me in converting a string like this:
"08/08/2007 09:48:02.265"
Into a Universal "u" time DateTime format?
I've tried many different variations of code but can't seem to make the conversion without it automatically being changed back to en-GB!
Thanks for reading, any suggestions will be greatly appreciated
Rob
Hi covertx,
As far as I know, if you would like to parse a date/time string and convert it to a UTC DateTime, use the DateTimeStyles enumeration AdjustToUniversal value with either the DateTime.Parse or DateTime.ParseExact method.
Some sample codes as follows for your reference:
string strDate = "08/08/2007 09:48:02.265";
DateTime dt0 = DateTime.Parse(strDate, null, DateTimeStyles.AdjustToUniversal);You can try to check out this document about "Formatting Date and Time for a Specific Culture" for details - http://msdn2.microsoft.com/en-us/library/5hh873ya(vs.80).aspx
Hope this helps,
Regards,
Citizens on the earth
Thursday, September 13, 2007 8:25 AM