covertx

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



Re: Visual C# General DateTime conversion

DongMT

DateTime DC ;

DC = DateTime.Parse("08/08/2007 09:48:02.265");

MessageBox.Show(DC.ToUniversalTime().ToString());






Re: Visual C# General DateTime conversion

covertx

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!





Re: Visual C# General DateTime conversion

DontFret

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 Snippet

TimeZone 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 Snippet

DateTime DC;

DC = 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.





Re: Visual C# General DateTime conversion

covertx

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 Snippet

foreach (string daystr in e.Days)

{

DateTime parsedDateTime = DateTime.Parse(daystr);

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





Re: Visual C# General DateTime conversion

covertx

Please forgive the probably AWFUL use of syntax for the numbered attempts!

Rob





Re: Visual C# General DateTime conversion

boban.s

DateTime localDate =

DateTime.ParseExact("08/08/2007 09:48:02.265", "MM/dd/yyyy HH:mmTongue Tieds.fff", CultureInfo.InvariantCulture);

DateTime universalDate = localDate.ToUniversalTime();

string universalDateString = universalDate.ToString("yyyy-MM-ddTHH:mmTongue Tieds.fff");

PS: Don't ever use DateTime.Parse






Re: Visual C# General DateTime conversion

Ryan Alford

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





Re: Visual C# General DateTime conversion

timvw

Personally, i choose NOT to use DateTime.Parse because it's not explicit enough (it depends on the current thread's CultureInfo)...





Re: Visual C# General DateTime conversion

boban.s

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.






Re: Visual C# General DateTime conversion

Ryan Alford

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.




Re: Visual C# General DateTime conversion

nobugz

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"





Re: Visual C# General DateTime conversion

covertx

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





Re: Visual C# General DateTime conversion

nobugz

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.





Re: Visual C# General DateTime conversion

covertx

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