How to get the *real* date using Moment.js? - javascript

So this has been on my mind for a long time - I was trying to "trick" my website and change my device's date. Unfortunately, this worked. I easily changed my laptop's date in the settings, entered the website and got the fake date.
So my question is simple, how do I get the real date?
This was the code I used: (The PST real day is 17th of August), and I changed my device's date to the 20th of August:
moment().tz('America/Los_Angeles').toDate().format('D') //returns 20

Related

Differences in dates between .Net and Javascript

I had an issue where dates were out by one day when displaying on an asp.net web form. These dates are only used for display so I can pass them as strings to resolved the issue, but I'm curious about why I'm seeing this behaviour.
I'm in Ireland, and while Ireland is more or less in line with GMT, we use IST (Irish Standard Time) during summer instead of DST and then revert to GMT for Winter. This has the same effect as being on GMT, but "officially" is slightly different.
As we're not on GMT, in the past, IST and DST didn't always line up.
For example, in 1958, IST started in the 20th April and ended on the 5th October whereas, DST started on the 27th of April and ended on 26th of October.
So if a date between the 5th and 26th of October 1958 is passed to JS, JS will display it as the previous day.
I wrote this this code to try and understand what's going on:
DateTime date = new DateTime(1958, 10, 4);
while (date <= new DateTime(1958, 10, 30))
{
Console.WriteLine($"normal : {date} | isDst? : {date.IsDaylightSavingTime()}");
Console.WriteLine($"universal: {date.ToUniversalTime()} | isDst? : {date.ToUniversalTime().IsDaylightSavingTime()}");
Console.WriteLine($"local : {date.ToLocalTime()} | isDst? : {date.ToLocalTime().IsDaylightSavingTime()}");
Console.WriteLine("-------------------------");
date = date.AddDays(1);
}
Which produced this output (truncated):
So I can see there are a number of days being mis identified as DST days, but it doesn't seem like that would cause this? If both .Net and JS though they were DST days, then surely the end result should be correct?
Additionally, why is there a 2 hour difference between the output of ToUniversalTime and ToLocalTime during DST?
Here's a screenshot of JS processing a few dates duirng this problematic window
You can see that JS (or chrome?) is aware that during the 5th to the 27th of that year, Ireland is no longer on GMT+1 (even though it still says it's IST) so why is the date passed from VB an incorrect date? I though they both got their datetime information from the same source i.e. the host pc?
You appear to be running .NET on Windows, in which case .NET is using the Windows time zone data for your local time zone.
The Windows time zone data does not have the full history of time zone changes from all time. Microsoft's policy only guarantees historical data is present for dates from 2010 forward, though some time zones have historical data before then.
Conversely, Chrome is using time zone data from ICU, which uses IANA time zone data. IANA time zones have historical data since at least 1970, though many time zones have historical data before then.
With specific regard to Ireland, IANA has Irish time zone data going back to 1880. Windows has no history for Ireland at all, so it assumes the current rule has always been in effect. In reality, the current rule has been in effect since Oct 1968, so any dates before then will only have accurate time zone information in the IANA data.
If you run the same .NET code you showed above on Linux or MacOS, you'll see that .NET will use IANA time zone data on those platforms and your results will match up for 1958. Or, if you pick a more recent date your results will match on Windows too.
In short - don't run these sorts of tests on old dates and expect to get the same fidelity that you'll get with modern dates.
You also asked:
Additionally, why is there a 2 hour difference between the output of ToUniversalTime and ToLocalTime during DST?
Your date variables are all DateTime where .Kind is DateTimeKind.Unspecified. The IsDaylightSavingTime method will treat such values as if they belonged to the local time zone, as if they actually had DateTimeKind.Local. The same is true for the ToUniversalTime method, however, the ToLocalTime method will assume that DateTime values with DateTimeKind.Unspecified kind are actually in terms of UTC - as if they were DateTimeKind.Utc. Thus, when DST is in effect, date.ToUniversalTime() shifts an hour backward, and date.ToLocalTime() shifts an hour forward.
You can avoid such ambiguities by using DateTimeOffset instead of DateTime.

MomentJS Accounting for Timestamp's DST and not current DST

I am using momentjs to account for timezone differences in an international application. I use it to parse ISO 8601 (ex. '2021-12-17T18:40:02.389Z') strings, and expect that the date and time be local to the client. I am running into an issue where it displays both CST and CDT, for example, in the application. It seems to be using the timezone/daylight savings of the timestamp, rather than converting it to the timezone we are currently in. For example, it's December as of this posting, so I would like all times to be displayed by moment in CDT, whereas in May, I would like it to be displayed in CST, regardless of when the timestamp was created. Is this possible with moment? I haven't been able to find out about this in their docs.

A Javascript based time picker control which converts the selected time into utc

I am looking for a JavaScript based time picker (jquery, extjs, bootstrap etc) control which converts the local time into UTC time. It must take into consideration user location and provide accurate time.
There are lot of controls available which allows you to pick time but I want to know what is the most accurate way to convert the time into UTC equivalent. There are lots of post available which talks about converting time into UTC but some of the post are old and some of them have one or the other catch.
What I am trying to do:
Assume the local time on user machine is 10 AM. I am asking user to pick a time > current time (or a future time). Assume user picks 11:30 AM. I want convert the 11:30 AM (local to his timezone) into UTC equivalent.

Convert set date to user's local time automatically

I am creating an application where I have a pretty big set of dates and times, and I need to display these in the user's local time and date. All set dates and times are in BST; so for example 08-24-2014 16:00 BST = 08-24-2014 11:00 EST. Now, before coming here I spent good 3-4 hours looking for an answer but if anything, I got more confused. Is there any way, to convert a set of BST dates and times to the user's local settings automatically without them setting the time zone etc?
p.s.: I have two ideas in mind but I don't know if they would work nor how to execute them.
1) Get and change the BST date and time and convert it to a unit of measurement; get and change the user's local date and time and covert it to the same unit of measurement as above, calculate the difference in the new measurement, and convert that to the user's local time.
2) Use GeoLocation to find the user's date and time/ time zone and; convert the BST to whatever the GeoLocation spits out.
you can get the users machine timezone in javascript:
var currentDate = new Date();
var currentTimeZoneOffsetInHours = currentDate.getTimezoneOffset() / 60;
see documentation here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTimezoneOffset
having the offset you can add it to your values
there is a javascript library Moment.js which allows you to query daylight saving values for specific dates and timezones.
because all your dates are in british summer time. first query if a date is in british winter time with isDSTShifted(). and subtract() an hour.
convert the date to another timezone

Jquery Date picker shows different day on phone vs PC?

I am baffled by this current situation.
My PC Shows the Jquery date picker properly. Where as accessing the same webpage from the phone shows the date pickers days as offset by 1.
I.e.
10/4/2012 = Tuesday on PC.
10/4/2012 = Monday on phone.
Things I have checked:
Both say April 2012 along the top.
The Date and time is set correctly on the
phone.
Both are using GMT London time.
Both are running from the same
page, hence same code.
This is because javascript is client-side not server-side.
So it takes the device date.

Categories

Resources