Moment.js local time zone issue - javascript

Using the code below, I am getting a Date from my database (SQL) and displaying it in a datapicker form field. The date displays fine for me but if I change my time zone (EST) in my system to one that is behind mine, the field will display the date as the day before. Does anyone know why this is occurring and how to fix it?
var NetNewBusinessDate = moment(model.NetNewBusinessDate).format("M/D/YYYY"));
model.NetNewBusinessDate == "/Date(1494561600000)/"

Found the answer here. Moment.js local relative time
The issue had to do with UTC conversion and Moment.js has an extension method that handles that. I just used moment.utc instead of moment and it worked like a charm!

Related

Timezone difference of browser and server

I have been using momentjs for detecting timezone of user (client side). I have PHP timezone_identifiers_list() function for server timezone list.
Issue I am facing right now is, momentjs is returning timezone as 'Asia/Calcutta' and PHP is returning it as 'Asia/Kolkata'. Both are same but giving me a reverse effect.
Is there any way to match both these? Any help will be much appreciated.
Try using UTC and then adding or subtracting time on/off based on location.
I have not used moment.js, but looking at the documentation http://momentjs.com/timezone/ it seems as you can get the time in zulu time.
dec.tz('Asia/Calcutta').format('ha z');
// where dec is your moment time.
In php you can do the same-ish
echo gmdate('H', strtotime(time()));
// this should return the + or - hours your server time is from UTC/zulu
With those two values you can calcluate what time it is at the user.

How to properly compare UTC dates against Local date in LINQ to Enitites?

I am having trouble getting data on a specified date using LINQ.
My dates are stored as UTC in the SQL database (DateTime.UtcNow).
I want to get all records for a date I or a user specifies on the browser (from a datepicker).
I am based in Australia and my database and site is hosted in another timezone and having trouble getting the results correctly.
I have tried using timezoneOffset from JavaScript and passing it to my controller to try converting UTC with the below code but still gives me incorrect results:
myRepo.Where(x => x.Date.AddMinutes(-(timezoneOffset)).Date == date.Date)
I've been scratching my head for almost 2 days already and haven't found a clear and proper solution online.
Help! :)

Moment js utc() time in london - BST

I'm using momentjs lib to updated text on some ajax action. What I need to do is to set a current date & time in london. I'm using moment.utc() function but because of the summer time I'm one hour out.
For example running this on 14:26
console.log( moment.utc().format('HH:mm:ss') );
I'm getting 13:26:53.
Any idea on how to fix this?
Can you use momentJS timezone?
moment().tz('Europe/London');
EDIT: In case you try to use this without seeing the link, it's a separate library you have to include.
If you want the local time instead of the UTC time, just use moment() instead of moment.utc(). You're specifically asking for UTC, so you shouldn't be surprised when you get UTC :)
From the documentation:
By default, moment parses and displays in local time.
If you want to parse or display a moment in UTC, you can use moment.utc() instead of moment().
This brings us to an interesting feature of Moment.js. UTC mode.
While in UTC mode, all display methods will display in UTC time instead of local time.
This is assuming you always want the user's local time. If you want a specific time zone (London) which may not be the user's time zone and isn't UTC, then you should use the library indicated by Takuya's answer. I would think carefully before doing so though - while it may be a sensible approach, you should at least validate that first. It's often reasonable to display a time for user U1 in the time zone of user U2 - but here you're using a fixed time zone. That's only appropriate if you know that U2 will always be in London. It would be really confusing if actually U2 is in some other zone - either the same as or different to U1.

Strange javascript Date toISOString

I am using fullcalendar for a project of mine. I have a strange problem that keeps bugging me. When I export an event, I want to get it in timestamp format, which is OK. the problem is when I try to convert it to ISO format using Date.toISOString function, it gives me the time 2 hours earlier. I think it is a problem of my timezone, because I posted my timestamp on http://www.unixtimestamp.com/ and it gives me the right time, but when I do it in my browser, it gives me the date and time with 2 hours earlier. I can't seem to figure it out. I googled a lot for a solution, but nothing so far. Does anybody have a clue?
toISOString will give you a string to represent the time in UTC. If you look at the result closely enough, you can find there is a "Z" in the end, which means the timezone is UTC.

Change timezone in Arshaw FullCalendar

From my understanding, Arshaw FullCalendar displays events according to the timezone of the local computer's operating system. I assume this is done by the javascript Date() object, which also is based on the local computer's operating system. I have an application that has group calendars and a group timezone, I want to be able to force every local Arshaw Calendar to display according to that group's time-zone, no matter what timezone that computer is. How do you do this?
Note: I've looked through the documentation fairly thoroughly, and found no such option. I'm hoping that javascript has something equivalent to php's date_default_timezone_set(), which seems to me the way this could be solved.
*Edit 1/31/2013 12:23pm CST:
I am sending everything to the calendar as unix timestamps, so I assume the option ignoreTimezone would not apply here, as described in this stackoverflow thread:
jQuery FullCalendar timezone synchronization
You should probably try to set the option "ignoreTimezone" to "false" and give to Arshaw FullCalendar date in ISO8601.
You can get more information about that here: http://arshaw.com/fullcalendar/docs/event_data/ignoreTimezone/
To convert unix timestamps to ISO8601, you can use this in javascript:
var d = new Date(1360412434000).toISOString();
This is ECMAScript 5.
See here for compatibility and fallback code: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date/toISOString

Categories

Resources