Reputation: 65
I need to convert a UTC DateTime
string to a ZonedDateTime
.
The UTC DateTime
strings can only be one pattern(M/dd/yyyy h:mm:ss tt)
as only this format is supported in a SharePoint DateTime
type column.
Assuming that (5/28/2013 1:00:00 PM)
as my UTC time, how can I convert it into ZonedDateTime
? I am currently using the below code to obtain the ZonedDateTime
.
var dateTimeZone = DateTimeZoneProviders.Tzdb[zoneID];
var inst = Instant.FromUtc(year, month, day, hour, minute);
ZonedDateTime dz = new ZonedDateTime(inst, dateTimeZone);
The above code needs the year
, month
, day
, hour
, and minute
as int
values. Is there an easier way to obtain the ZonedDateTime
directly from the UTC string?
Upvotes: 3
Views: 2126
Reputation: 241563
I think you are confused about your source data. SharePoint doesn't store strings for dates. It stores them as DateTime
types. They are at UTC, but they aren't strings.
If you have a string representation, then at some point in your code you did a .ToString()
or similar, and it picked up the default "G"
format specifier.
So you should just be able to use:
var inst = Instant.FromDateTimeUtc(yourDateTime);
var dz = new ZonedDateTime(inst, dateTimeZone);
The other option you have is to use DateTimeOffset
fields in SharePoint instead of UTC DateTime
fields. Read here. Then you can use Instant.FromDateTimeOffset()
.
Upvotes: 4