3

how to convert datetime string to utc time format in GMT.

var x = "02/01/2017 10:00";
var z = DateTime.ParseExact(x, "ddd, dd MMM yyyy HH:mm:ss 'GMT'", CultureInfo.InvariantCulture);
1
  • 1
    Unclear what you are asking. Please specify what you expect as an output Commented Feb 2, 2017 at 1:41

3 Answers 3

9

Actually the thing you have tried is wrong, what you can do is, get the DateTime value equivalent to the given date and then convert them to the UTC time, try something like the following:

string inputDateStr = "02/01/2017 10:00";
DateTime inputDate;
if (DateTime.TryParseExact(inputDateStr, "MM/dd/yyyy HH:mm", CultureInfo.InvariantCulture, DateTimeStyles.None, out inputDate))
{
    Console.WriteLine("Date time Now  : {0} ", inputDate);
    Console.WriteLine("Date time UTC  : {0} ", inputDate.ToUniversalTime().ToString("ddd, dd MMM yyyy HH:mm:ss 'GMT'"));                
}

Working Example

Sign up to request clarification or add additional context in comments.

3 Comments

Hi. Thanks for working example. how can I get inputDate in datetime rather than string.
if the input is in DateTime, then use inputDate.ToUniversalTime().ToString("ddd, dd MMM yyyy HH:mm:ss 'GMT'") only no need for converting
I need inputDateTime in datetime format. so i did inputDate.ToUniversalTime().ToString("ddd, dd MMM yyyy HH:mm:ss 'GMT'").ToDateTime(), to convert to datetime.
3

It's not completely clear what you are asking... UTC isn't a format, it's a timezone, equivalent to GMT (well, actually that isn't strictly true, but for the purposes of this question it should be ok.

What point in time is your string representing? Is it UTC? Or is it the local time in Moscow? You need to have the answer to that, because they are completely different times.

If the string represents a UTC time, then you can do something like this:

// first parse it to a DateTime object. Notice that the format string corresponds with the string you have - in your code, they were completely different.

var dateTimeWithUnspecifiedKind = DateTime.ParseExact(x, "dd/MM/yyyy HH:mm", CultureInfo.InvariantCulture);

So how you have a datetime object. It has a "Kind" property of Unspecified. So it isn't storing any information to specify that it's supposed to be UTC, rather than Moscow or New York time.

// so we do this...

var dateTimeAsUtc = DateTime.SpecifyKind(dateTimeWithUnspecifiedKind, DateTimeKind.Utc);

That means, "keep the numbers the same, but make a note that the datetime is to be interpreted as a UTC datetime".

Then, if you want to convert it back into a string, you can do something like:

var s = dateTimeAsUtc.ToString("O");

Which will give you a nice representation:

2017-01-02T10:00:00.0000000Z

You are lucky you are interested in UTC. The datetime class can only do UTC, "local" (whatever that is), or "Unspecified". It's not super useful for this sort of thing. DateTimeOffset is a bit better - it's basically a DateTime but it also stores the difference between UTC and the offset that's in force at the time.

That may be all you need. If you ever need real clarity around this stuff though, take a look at Noda time - an alternative set of date and time classes for .NET.

2 Comments

Please format the code sections using {} button so that your post is more readable.
Sorry - it's my first post - I'll read up on the formatting before next time!
1

I am not sure what you wanted to do. But, Universal Time, Zulu time, and UTC are effectively modern names for Greenwich Mean Time (GMT). So both the times will be same. You can use the DateTime.ToUniversalTime() or DateTime.UtcNow method to get the UTC time.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.