Ajax post javascript date as UTC OR server Time - javascript

I am trying to post a created date to an ASP.Net MVC controller without the date being modified by the serializer. I am looking for some kind of way to do this on the client.
The date is being constructed as follows:
var priceDate = new Date(name.split("-")[1], name.split("-")[0]-1, 1);
The date is valid and the problem is the serializer is adding the timezone offset which i don't want. The javascript date should be UTC but this doesn't seem possible.
This problem is expressed in different ways all over the place with all kinds of solutions that just seem way over the top. Is is possible to make a javascript date UTC or devoid of timezone information from the client?

There is toISOString() function which returns dates in standardized format and always does zero time zone.
Server accepts dates in local zone, so you need to do next date.ToUniversalTime().
So javascrript date.toISOString() equals to c# date.ToUniversalTime().

Related

Dealing with js localized date in C#

My app (back-end in C# & front-end in Angular Materials) has a search screen allowing user to specify the date period using datepickers. The problem is that some of the users are not in UK while all the data they view has been created with GMT date. So if someone in Germany selects date 01/01/2017 in datepicker, my back-end reads it as 31/12/2016 23:00:00 resulting in incorrect search results.
Can someone advise me how to deal with this? I'd like to still use the Angular Material datepicker but be sure that I'm passing the date selected by the user. I know I can transform the date before posting it like this:
moment(myDate).format('MM/DD/YYYY'))
but I have a lot of cases like this and would prefer some generic solution.
For transmission and storage, I advise using UTC for everything. Only at the point of display should the time be converted to whatever locale the user has selected. Despite this being an old problem, running into time conversion issues is still quite common. Most places I've worked at will store everything as UTC timestamps or Unix epoch time with respect to UTC, that way there is no question what the meaning is anywhere in the system. If/when it needs to be rendered to something local, we do it on the client side.
For example, to get the local time converted to UTC as a string:
var noTimeZone = new Date().toUTCString();
-or-
var noTimeZone = new Date().toISOString();
Or, if you want a numeric value so you don't have to deal with funky format parsing between client/server, you can get the Unix epoch:
var unixEpochMS = new Date().getTime();
Mind you, Date.getTime() will return milliseconds rather than seconds. Also note that the Unix epoch is defined in terms of UTC. That is, any numeric value that is a timestamp is expected to be UTC. If you want a different timezone, you need to parse the value and then set the timezone to what you want.
Solution 1:
I think the solution is to get your user's timezone. You can use Javascript to get timezone from user's computer and send it to server with the request.
var d = new Date();
var tz = d.getTimezoneOffset()/-60;
tz will be 2 if user's timezone is GM+2
Soution 2:
You send and receive Unix timestamp. But then you need to convert the timestamp to readable date/time based on user's timezone.

Javascript handle daylight savings time for different time zones

Here I got an input box on my web page for users to input a date. For some reason I can't use a datetime picker and I have to pass it to an .NET based service as a String via ajax.
Users may from different time zones. And the date is stored as UTC in database.
It seems I have 2 options to handle the timezone:
Convert the date string to UTC date string at frontend and pass to service.
Pass the UTC offset to service and convert the date string to UTC at backend.
However, neither option can handle the daylight savings time.
Could anyone give me some suggestions on this?
Javascript's toUTCString() and functions like getUTCDate() instead of getDate() will ignore the timezone offset, including I presume the DST offsets.

javascript: convert date and time strings to UTC

I have had a truly gnarly time trying to work with Date objects in javascript, which strike me as ugly and unintuitive.
I am getting back two objects from an internal API: one date string in the form of YYYY-MM-DD and one time string in the form of HH:MM.
What I want to do, in javascript, is to merge these into a UTC-formatted string of the form YYYY-MM-DDTHH:MM:00Z. I have been playing around a bit with the moment.js and moment timezone libraries, but I'm not sure how I might leverage these to complete this task.
I solved this thusly using moment timezone:
dateTime = date.concat(" ").time
dateTimeAdjusted = moment.tz(dateTime, timezone).format()
(where timezone was a variable that I had access to that contained the timezone of the given date and time.)

Set default TimeZone of the browser window in javascript

We are displaying schedules on our webpage which is build on GWT. Client system using different timezone from server and because of that, all the schedules were displaying wrong. Is there a way to set default time zone when we load the page? Like the way we do it in java:
TimeZone.setDefault(TimeZone.getTimeZone("Asia/Kolkata"));
Thanks!!!
No, you can't set the timezone of Date objects in javascript. Usually you use only UTC and epoch-based timestamps.
Only when creating a Date from a string or from year, month etc. the local timezone will be used, you can only get the timezone offset.
Converting a timezone can only be done by re-setting the Hours of the Date object (example described here), creating a date which looks-like having an offset timezone but is just utc.
In case you are using moment.js for your dates, you can set the default timezone for all newly created moments with:
moment.tz.setDefault(String)
https://momentjs.com/timezone/docs/#/using-timezones/default-timezone/

javascript dateTime - the same format as server

I have an ajax call, which returns datetime. Javascript displays it using client timezone. I not need in any client timezone, I want to show datetime the same as server return. Is it possible?
I get date via:
var d = eval('new' + date.replace(/\//g, ' '));
First off, there's no reason to use eval here (or almost anywhere), and generally lots of reasons not to.
JavaScript has only "local" time (the timezone of the client) and UTC (universal coordinated time, effectively the same as GMT). So your best bet normally is to have the server send you the time in UTC. But in your case, since you want to display the date in the server's timezone, it doesn't really matter whether JavaScript thinks the date is in local time or server time, and it's fine to send it in the server's timezone.
Note that when parsing date strings, JavaScript only recently (as of ECMAScript5) got a standard format for date strings, which is a simplified version of ISO-8601. Details here. Note that some older browsers will not yet support that format.
It's impossible to offer much more guidance without an example of what you're trying to parse.
Diodeus' suggestion also seems to me to make a lot of sense: If you want the date to be displayed in the server's timezone and format, just display the string you're given without interpreting it (again, subject to what the string looks like; I can't immediately come up with a reasonable format that would work with your posted code).

Categories