I have this weird format of dates 1640828010231 which I'm trying to convert to actual date using javascript. I'm using the following code.
new Date('1640828010231')
but it is giving me the current time. I have no idea what format that date is in. I have tried various ways. please help.
It's timestamp,
Have a look to mozilla doc to understand better https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#the_ecmascript_epoch_and_timestamps
In my app, I retrieve time from API in a format like this : 2020-08-31T15:05:10.768904Z and I'm trying to compare this date with the current Date().
What is the best way to calculate difference between the two.
Thanks!
Sorry, that's not in comment because I cant yet. You have to decompose it and i would use regex to get it.
I need to store in the backend a date in the format yyyy-mm-dd;
I've initially used in the frontend a js Date() object to store it, and dropping the time and timezone info while sending it to the backend; but that caused a "timezone issue" when parsing back the yyyy-mm-dd string into Date();
even if it's possible to workaround that issue I was wondering if using a simple String wouldn't be better; is there any drawback that now I cannot see and so I should use a Date anyway?
Similar to other comments, I would also recommend sticking with Date instead of strings to prevent browser differences and for ease of use. One note is when setting the date, don't forget to account for day-light savings which might cause a database crash if not accounted for.
CSS-Tricks: Everything you need to know about date in javascript
One of our programmers decided to use a DATE field in the MySQL db in order to achieve this.
Sending and saving a JS date object did work well until the daylight saving changes intervened (with nasty effects :) ).
Of course, saving the date in a DATETIME field solves it, but everybody sees the time/dates in their own timezone.
We need everybody (all over the timezones) to see the same date!
I clarify this, to get the proper answers:
I want to keep using the DATE field storage type in MySQL (vs DATETIME - ok, maybe too much of an optimization, but it's already there and I want a long term solution for when I receive such structure/code from other developers)
Sending local time (local JS in browser) 23-05-2016, will reach the server as 22-05-2016 0X:X0:00Z (UTC) and be store as such. Because it's a DATE field, the stored value will become 22-05-2016 only. And you lost a day! :)
Our solution from bellow not only fixes the DATE field trimming, but also adds the fact that people now can see the same correct date (23-05-2016) no matter of the timezone they are in!
I like the outcome and would love to see some better solutions to achieve the same and improve the system.
Actually, we have noticed the problem only when the daylight saving time changed, so my solution (as answer bellow) is a good solution for that as well. And it only consumes resources client-side.
I have posted my own solution to this question as an answer bellow.
It would be really cool to see a much better solution from you!
With Javascript
Save your dates in ISO format (including timezone information) and use moment.js to convert the datetime to another timezone.
If moment.js is not already a dependency, and you want to avoid extra libraries, keep reading.
With MySQL
Instead of solving this problem when you write the data (losing timezone information in the process), solve it when you read the database.
In your SELECT query, normalize all DATETIME values to your preferred timezone using the convert_tz built-in function.
MomentJs is your best bet. Find the timezone you want and pass the ISO string to it and you should be good to go.
http://momentjs.com/timezone/docs/#/using-timezones/
A DATE is just a year, month, and day. It doesn't have a time, or a time zone. Think about your birthday or your wedding date, or today's date.
The JS Date object is not this at all. It's a timestamp. It's the number of milliseconds elapsed since Midnight January 1st 1970 UTC.
You should leave your date as a date-only wherever possible. Use the ISO-8601 date-only format, which is YYYY-MM-DD. If you have to assign it a time and time zone, then be very careful when you do.
If you just assign midnight local time, then you're risking losing a day (as you showed), and you're not considering that there are local days in some time zones where midnight does not exist! (Such as the spring-forward day in Brazil). Noon is a safer bet than Midnight, but still you should use this sparingly. The better approach is to keep dates as dates, not as date-times.
Also, I'd answer with code if I could, but you didn't provide any code in your question showing what was broken. Please read How do I ask a good question? and How to create a Minimal, Complete, and Verifiable example. Thanks.
There are more solutions to this, but the fastest and easiest that I could come up with is described bellow:
Let's intervene as early as possible in the information stream.
Just change the data before transmitting it through AJAX.
The function we used is this:
function addTimezoneDiffAnd12HoursToDate(date) {
var timezoneOffset = date.getTimezoneOffset();
date.setHours(12-Math.floor(timezoneOffset/60));
date.setMinutes(-timezoneOffset % 60);
return date;
}
What it does is that it converts a Date to be always at noon (12:00) UTC!
You can use it like this:
$scope.contract.contractDate = addTimezoneDiffAnd12HoursToDate($scope.contract.contractDate);
and send it as such to be stored in the DATE field.
Let me know if you have a simpler solution. I'd like to see it.
I am wondering if somebody can offer me help here.
I am working with a JSON file to create a timeline using AngularJS. I have been able to figure out how to output the date using the HTML binding ng-repeat loop, but I need to edit the date output; "2013-01-01" becomes "December 31, 2012" after I reformat it.
I have been struggling to figure out how to pull the date value IN THE CONTROLLER so that I can apply it in a function that will turn that value into the proper date -- by adding another day to the date as it is.
Thank you very much! I hope that made sense.
Javascript automatically localizes a date. The date filter in angular expects a timezone and when one is not provided assumes UTC. The issue is that once your timezone-less (thus assumed UTC) string gets localized, it is no longer precisely 2013-01-01, but however many hours shy of that your timezone is. Here in Pacific Standard Time, that's -8. So the date appears to be the day before.
Data from the server should have a timezone associated with it - that's just best practice. Without it, assuming UTC is a good bet. After all, some standard is needed.
Here's a Plunker showing a few examples of how this works, should you need them: http://plnkr.co/edit/Jhwnkj?p=preview
Use Angular date Filter in your view. You don't have to change any value in the controller.
Here is the example for you http://plnkr.co/edit/iqIpW5uADTRVFycDxdBa
Read AngularJS date filter.
http://docs.angularjs.org/api/ng.filter:date