Hey all!

Here's the scenario:

  • I've got an array of strings (Dates, but stored in a string[])
  • For each date in this array, I need to parse it as a universal DateTime EG "yyyy-MM-ddTHH:mm:ss.fff"

Because it's a string, I can't perform the parse AND make the format conversion, so I tried parsing the date string as a DateTime WITHOUT performing the format conversion... This is fine...

Now that I've got my DateTime in a culturaly biased format, I can convert to the "yyyy-MM-ddTHH:mm:ss.fff"
but when I try to parse a final DateTime value (called newDateTime) it automatically converts it back to my cultural DateTime!

Any suggestions to remedy this?

Many thanks for reading


Rob

NB. Below is my code... dateCollection is a collections of dates in universal DateTime format


foreach (string daystr in e.Days)
{
DateTime parsedDateTime = DateTime.Parse(daystr);
string newDateTime = parsedDateTime.ToString("yyyy-MM-ddTHH:mm:ss.fff");//.ToString("u");
DateTime date = DateTime.Parse(newDateTime, CultureInfo.InvariantCulture);

if (!dateCollection.Contains(date))
{
dateCollection.Add(date);
}
}

Recommended Answers

All 2 Replies

Change your machine time format

Control Panel->Regional and language options

What i don't understand in your code is that you first format the DateTime to a string and then you construct another DateTime object from that same string; is that step absolutely necessary? By reading that small snippet you posted I can't see any obvious reason why one should do that, imo this adds overhead and confusion to the code.

If you need to do some conversion from local time to utc time or vice versa, have a look at DateTime.ToLocalTime or DateTime.ToUniversalTime methods

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.