Filmjamr Movies
Filmjamr Movies

Reputation: 987

How to Convert Date Time using Offset values

I Need to convert a DateTime accordign to the given offset value. I have values like:

-08:00

-07:00

+08:00

+07:00

+02:00

+05:30

How I can convert a date time according to these given offset values. I tried:

string t = "Pacific Standard Time";
var tInfo = TimeZoneInfo.FindSystemTimeZoneById(t);
var data = TimeZoneInfo.ConvertTime(DateTime.Now, tInfo);

this is fine to but I need to convert based on offset value.

Plz suggest here how to convert it.

Thanks

Upvotes: 0

Views: 3202

Answers (2)

Panagiotis Kanavos
Panagiotis Kanavos

Reputation: 131393

The type that stores timezone offsets is DateTimeOffset. If you care about offsets use that type instead of DateTime. Otherwise you won't be able to compare dates with different offsets unless you also store the offset along with the datetime.

Properties like DateTimeOffset.Now and DateTimeOffset.UtcNow will return the current time in the machine's timezone or UTC respectively.

Creating a DateTimeOffset from a DateTime in a specific offset is as simple as calling the appropriate constructor, eg :

var pstTime=new DateTimeOffset(2019,1,14,1,0,0,new TimeSpan(-8,0,0));

You can convert a DateTimeOffset value to different offsets with ToOffset, eg :

var eastEuropeTime=pstTime.ToOffset(new TimeSpan(2,0,0));

This will return 11am.

ToUniversalTime will return the time in UTC, ie +00:00:

var utcTime=pstTime.ToUniversalTime();

Which returns 9 am.

Conversion from/to DateTime

You can also construct a DateTimeOffset from an existing DateTime value. The constructor check the input DateTime's Kind property and refuses to use invalid offsets if DateTime.Kind.

For Utc the only valid value is 00:00.

var now=DateTime.UtcNow;
var utcOffset=new DateTimeOffset(now, TimeSpan.Zero);
utcOffset.Dump();

For a local time, the only valid value is the machine's offset. To convert a local time to DateTimeOffset, you can retrieve its offset from TimeZoneInfo.Local :

var now=DateTime.Now;
var localOffset=new DateTimeOffset(now, TimeZoneInfo.Local.GetUtcOffset(now));

Arbitrary Offsets

In some cases we may want to use a DateTime as is with a specific offset. Many web sites (especially forums) for example allow users to specify their timezone and thus avoid problems with timezone detection on the browser.

In other cases, the offset may be stored separately or come from an IANA timezone name.

To use an arbitrary offset, the DateTime's Kind must have the Unspecified:

var date=new DateTime(2019,01,15,18.17.00, DateTimeKind.Unspecified);
var pst=new DateTimeOffset(date,new TimeSpan(-8,0,0));

This is useful eg when loading datetime values from a database. DateTime values loaded from a database are Unspecified.

Values that come from code or input controls may already be Local or UTC. In this case we need to use DateTime.SpecifyKind to create a new Unspecified DateTime value :

var date=DateTime.SpecifyKind(domeDate,DateTimeKind.Unspecified);
var pst=new DateTimeOffset(date,new TimeSpan(-8,0,0));

Upvotes: 3

Jamie Rees
Jamie Rees

Reputation: 8183

What you can use is DateTimeOffset.Parse

For example:

var offsetValue = "+08:00";
var now = DateTime.Now;
var nowString = now.ToString("dd/MM/yyyy HH:mm:ss");
var offsetDateTime = DateTimeOffset.Parse($"{nowString} {offsetValue}");

Please note the above is just an example, this is ignoring any sort of culture specific issues etc.

Upvotes: 0

Related Questions