1

I have a UTC timestamp created with the DateTime class.

var time = DateTime.UtcNow;

When I format it I get the following result:

time.ToString("yyyy-MM-ddTHH:mm:sszzz")
> 2019-02-03T10:08:40+00:00

Which is correct but I want it to be formatted in the local timezone like this:

2019-02-03T11:08:40+01:00 < note the +01:00

How can I get this?

I could use DateTime.Local in this example but the timestamp is actually coming from another system/module and so the solution has to work on something like DateTime.UtcNow

LHolleman
  • 2,486
  • 2
  • 18
  • 19
  • From [docs](https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings#the-zzz-custom-format-specifier): "With `DateTime` values, the `"zzz"` custom format specifier represents the signed offset of the **local operating system's time zone** from UTC, measured in hours and minutes. It does not reflect the value of an instance's `DateTime.Kind` property". I think your local timezone on the server you are executing that code is _already_ UTC+0. I would suggest to check it one more time. – Soner Gönül Feb 04 '19 at 10:16
  • I get these results on my own PC and I'm 100% sure that time is in +1. – LHolleman Feb 04 '19 at 10:18
  • As long as the other system is generating the time with a timezone it doesn't matter if it is UTC or any other timezone. Any DateTime is stored in Net as a UTC time. Net will automatically convert the the datetime to correct value when you use PARSE. Using UtcNow will give you issues because it will store the time incorrectly. What you really want is something like this : DateTime date = DateTime.Now; Console.WriteLine(date.ToUniversalTime()); – jdweng Feb 04 '19 at 10:20
  • With `DateTime` values, you should be using `K`, not `zzz`. Use `zzz` with `DateTimeOffset` values. And if you want local, then use `DateTime.Now`, or `.ToLocalTime()` first. – Matt Johnson-Pint Feb 04 '19 at 17:46

3 Answers3

2

I've done a little testing but I believe you will need to use DateTime.Now as opposed to DateTime.UtcNow.

When you use the 'z' characters in the format the conversion takes into account that you want a time offset and changes the time accordingly.

Give the following a try and let me know if it works for you:

DateTime.Now.ToString("yyyy-MM-ddTHH:MM:sszzzz")

This gives me 2019-02-04T10:02:32+00:00 (I am in a UTC Time Zone though)

Jacob JA Shanks
  • 370
  • 1
  • 13
  • If your wanting to adjust the timezone deliberately to "+1" you can always use a [DateTimeOffset](https://learn.microsoft.com/en-us/dotnet/standard/datetime/converting-between-time-zones) – Jacob JA Shanks Feb 04 '19 at 10:30
1

DateTime.UtcNow gives current time at UTC where time offset zone is 0. Change to DateTime.Now to get local time instead.

DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss K")
// 2019-02-04T08:36:11 -02:00

DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ss K")
// 2019-02-04T10:36:22 Z
Marcelo Vismari
  • 1,147
  • 7
  • 15
1

If you are interested in converting a UTC DateTime into a specific timezone's local time you can take a look at the TimeZoneInfo class. Here is an example:

var utcDate = DateTime.UtcNow;

// 04/02/2019 11:23:39
// Kind is Utc

var localDate = utcDate.ToLocalTime();

// 04/02/2019 12:23:39
// Kind is now local

var timeZoneId = "US Eastern Standard Time";

var timeZone = TimeZoneInfo.FindSystemTimeZoneById(timeZoneId);

DateTime usEasternStandardDateTime = TimeZoneInfo.ConvertTimeFromUtc(utcDate, timeZone);

// 04/02/2019 06:23:39
// Kind is unspecified

Also you can take a look at Convert UTC/GMT time to local time for more details and List of Timezone ID's for use with FindTimeZoneById() in C#? for a complete list of supported timezones

p1va
  • 86
  • 1
  • 3