0

I am building a scheduler application. The front end is developed using JavaScript and at back-end we are using .NET with SQL server.

User will be asked to select a time (which will be local to his machine). I want to convert that time into Eastern Standard Time.

The scheduler application will run based on EST.

I will save the local time selected by user, Time Zone info of the user and the EST equivalent of the time selected by user.

I can get the TimeZone by using following fiddle

var tz = jstz.determine();
console.log(tz.name());

To get the EST equivalent time, I will use TimeZoneInfo class When DST changes takes place, I will have a store procedure to update the respective EST time.

Let me know if this is valid approach?

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
SharpCoder
  • 18,279
  • 43
  • 153
  • 249
  • 1
    Saying *Country time zone* is really talking about something that doesn't exist. The USA has 4 times zones... so which one? I would recommend you just use *time zone*. – Erik Philips Nov 19 '15 at 20:18
  • @ErikPhilips: Since I will be running the server from newwork, I mentioned eastern time. So if we take EST as the base timezone, what should be my approach? – SharpCoder Nov 19 '15 at 20:43
  • You pretty much [asked the same thing yesterday](http://stackoverflow.com/q/33765831/634824). If you're wanting something more specific, then you need to be more specific in the ask. Show some code. What worked, what didn't, what did you expect, etc. – Matt Johnson-Pint Nov 19 '15 at 23:44
  • And FYI, if your solution at all depends on the time zone of the server, you will have problems. – Matt Johnson-Pint Nov 19 '15 at 23:45
  • @MattJohnson: I have updated my question. Please let me know your thoughts. – SharpCoder Nov 20 '15 at 22:18

2 Answers2

1

User will be asked to select a time (which will be local to his machine).

That's fine. For a scheduling app, that makes sense.

I want to convert that time into Eastern Standard Time.

The scheduler application will run based on EST.

That's not a good idea. The location of the server should be irrelevant. In particular, US Eastern Time fluctuates between EST and EDT. Basing it on EST would only take half of that into account, and basing it on ET would mean handling the fluctuations in your code.

In general, your server should be set to UTC, but the code running on the server should not care what time zone the server is set to.

Use DateTime.UtcNow on the server to get the current UTC time. The value will be the same regardless of the server's time zone.

I will save the local time selected by user, Time Zone info of the user and the EST equivalent of the time selected by user.

Yes on the first two. For the third, you should store the UTC equivalent - not the EST equivalent.

You also need to be prepared to recalculate the UTC value if the time zone rules change before the event occurs.

I can get the TimeZone by using following fiddle ...

jstz will guess at the user's time zone - but it is just a guess. It could very well be wrong. It's great for picking the default option from a list of time zones, but it should not be used directly in a way that the user can't override.

Additionally, it returns an IANA TZDB identifier, such as America/Los_Angeles. These do not currently work with TimeZoneInfo on Windows. To use these, you'll need to use Noda Time in your C# code.

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
-1

Use this

var utc_date = new Date().toISOString();

It will return a UTC formated string like 2015-11-19T19:41:45.865Z

Zohaib Ijaz
  • 21,926
  • 7
  • 38
  • 60
  • and if you are expert at C# or C++ then you can apply required timezone – Zohaib Ijaz Nov 19 '15 at 19:43
  • 1
    You can use this library if you have some other complex timezone conversions http://momentjs.com/timezone/ – Zohaib Ijaz Nov 19 '15 at 19:47
  • i don't know who voted down for me but I just want to ask that can you tell me the reason? – Zohaib Ijaz Nov 19 '15 at 20:00
  • I didn't downvote this one, but I assume that it's because the OP asked about converting with a *specific* time zone, not necessarily the user's local time zone. – Matt Johnson-Pint Nov 20 '15 at 04:37
  • He said this ` I want to convert that time into UTC or to a time zone specific to a country`, so I supposed that he can send UTC date string to backend and then he can apply timezone. I answered as he asked in first part `I want to convert that time into UTC` – Zohaib Ijaz Nov 20 '15 at 07:24