Richard
Richard

Reputation: 123

Converting datetime to a custom format

I am trying to convert a datetime which is in the format with a timezone to a specified custom format with only date and time. I am able to convert it but the result is shown completely different. For example:

string dateTime = Convert.ToDateTime ("2020-08-25T23:55:00+00:00").ToString ("yyyy-MM-dd HH:mm");
Debug.Log (dateTime);

The result in the log I get is : 2020-08-26 01:55

It shows result as the next day with totally additionally 2 hours time. What am I doing wrong here?

Upvotes: 0

Views: 105

Answers (1)

Kostasmel
Kostasmel

Reputation: 46

I think the problem is because of your Timezone. That's why you have different datetime after conversion. Try to use Parse instead of Convert method.

using System.Globalization;

var dateTime = DateTime.Parse ("2020-08-25T23:55:00+00:00", null, DateTimeStyles.AdjustToUniversal).ToString ("yyyy-MM-dd HH:mm");;
Debug.Log (dateTime);

Upvotes: 1

Related Questions