I have a string in this format: "20111027", i.e. of the general format: "yyyyMMdd".
How do I convert this to a DateTime having the timezone GMT?
This code does some conversion, but it's unclear what timezone would be used:
DateTime date = DateTime.ParseExact(dateString, "yyyyMMdd",
CultureInfo.InvariantCulture);
Use a DateTimeStyles of AssumeUniversal:
DateTime date = DateTime.ParseExact(dateString, "yyyyMMdd",
CultureInfo.InvariantCulture,
DateTimeStyles.AssumeUniversal);
From the docs of DateTimeStyles.AssumeUniversal:
If no time zone is specified in the parsed string, the string is assumed to denote a UTC.
Sounds like exactly what you want :)
(Alternatively you could use Noda Time and parse it to a LocalDate. It only represents a date, after all, so why use a type which cares about times and time zones? :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With