How can I parse a date such as the following and convert it to a Unix timestamp using JavaScript?
Sat Mar 29 2014 16:10:00 GMT+0800 (Taipei Standard Time)
Thanks.
you just need a good date-parsing function, I would look at date.js . It will take just about any date string you can throw at it, and return you a JavaScript Date object.
Once you have a Date object, you can call its getTime()
method, which will give you milliseconds since January 1, 1970. Just divide that result by 1000 to get the unix
timestamp value.
In code, just include date.js, then:
var unixtime = Date.parse("24-Nov-2009 17:57:35")
.getTime()/1000
Get date.js from http://www.datejs.com/
More here: https://stackoverflow.com/a/1792009/390897
Related
Doing this with the date-functions.js library (used e.g. in datetimepicker jQuery plugin):
Date.parseDate('2018-03-10 12:12', 'Y-m-d H:i')
gives:
Sat Mar 10 2018 12:12:00 GMT+0100 (Paris, Madrid)
How to get the result as Unix timestamp or GMT / UTC time instead?
A string like '2018-03-10 12:12' will usually be parsed as local as there is no timezone offset. It's also not ISO 8601 compliant so using the built-in parser will yield different results in different browsers.
While you can use a library, to parse it as UTC and get the time value is just 2 lines of code:
function toUTCTimeValue(s) {
var b = s.split(/\D/);
return Date.UTC(b[0],b[1]-1,b[2],b[3],b[4]);
}
// As time value
console.log(toUTCTimeValue('2018-03-10 12:12'));
// Convert to Date object and print as timestamp
console.log(new Date(toUTCTimeValue('2018-03-10 12:12')).toISOString());
var date = new Date('2018-03-10 12:12'.replace(' ', 'T'));
// Unix
console.log(Math.floor(date.getTime() / 1000));
// UTC
console.log(date.toUTCString());
As always, please have a look at the documentation at MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
Use MomentJS instead. You can specify exactly what format the string you're parsing is in. MomentJS can then provide you with the underlying Date object, unix timestamp as well as convert to UTC.
var d = moment('2018-03-10 12:12', 'YYYY-MM-DD HH:mm');
console.log(d.toDate());
console.log(d.unix());
console.log(d.utc().toDate());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.21.0/moment.min.js"></script>
You could of course also parse the date as UTC too instead of treating it as a local time.
moment.utc('2018-03-10 12:12', 'YYYY-MM-DD HH:mm');
NOTE Bit difficult for me to test UTC as I'm in the UK and GMT and UTC are virtually the same.
I'm using the following code to get current date and time in nodejs.
var date = (new Date()).toJSON();
after converting to JSON, it returns a wrong time with a wrong timezone as below:
2018-01-03T11:16:38.773Z
but without toJSON() it returns the real time in correct timezone
Wed Jan 03 2018 14:47:12 GMT+0330 (Iran Standard Time)
The format is different because:
The toJSON method is a built-in member of the Date JavaScript object.
It returns an ISO-formatted date string for the UTC time zone (denoted
by the suffix Z).
What you can do:
You can override the toJSON method for the Date type, or define a
toJSON method for other object types to achieve transformation of data
for the specific object type before JSON serialization.
source
If you want the same result you could just use toString instead of toJSON:
var date = new Date().toString();
2018-01-03T11:17:12.000Z === Wed Jan 03 2018 14:47:12 GMT+0330 (Iran Standard Time)
The one on the left hand side is ISO timezone and one on the right is basically the browser timezone.
(new Date()).toJSON() converts into ISO timezone
So a simple way to convert to string is
var date = (new Date()).toString();
I need to produce a timestamp of each date/time string that gets produced in a foreach loop.
How do I turn this string Mon Aug 08 2016 10:09:42 GMT+0100 (BST) into a Unix Timestamp for comparison?
I am then going to use that single value to do a jQuery sort (code below)
var boards = $(".socialBox");
boards.sort(function(a, b){
return $(a).data("date") - $(b).data("date");
});
$("#social-board").html(boards);
As you can imagine the above code doesn't work on the current date/time string.
You can convert the string data to date object along with .getTime() to get number of milliseconds since 1970/01/01:
boards.sort(function(a, b){
return new Date($(a).data("date")).getTime() - new Date($(b).data("date")).getTime();
});
You can try this:
new Date('Mon Aug 08 2016 10:09:42 GMT+0100 (BST)').getTime();
The getTime() function returns the number of milliseconds since 1970/01/01
I have a date in the following format
Fri Mar 16 2012 05:53:18 GMT 0200 (GTB Standard Time)
And I want to convert it into a unix timestamp.
Until now I manually split the string by spaces and then I am giving it as an input to a Date object, in order to get milliseconds in a latter step.
Is there any easiest way?
(I am trying to avoid jQuery plug-ins and do it using vanila javascript)
Yes, the easiest way would be to pass the string to Date object and then call the getTime method:
var myDate = new Date('Fri Mar 16 2012 05:53:18 GMT+0200 (GTB Standard Time)');
console.log( myDate.getTime() ); //1331869998000
No need to split your string by spaces.
I am having issue with the localtime zone things in javascript. If I got a string value from the server is "2014-02-03T00:00:00.000Z", once I pass it into Date object new Date('2014-02-03T00:00:00.000Z'), the new date object will be in localtime zone ex. Sun Feb 02 2014 18:00:00 GMT-0600 (CST). How to keep the value as 'Mon Feb 03 2014 00:00:00' ? I see a lot of people is using moment.js for dealing date, but I don't find any help with this issue.
Thanks
You can use getUTCDate() method. It will return you correct date.
http://jsbin.com/zizukapuba/1/edit?output
It will convert the date into required format with reference to system local timezone.
NOTE: If you use, the getISOString() method, then it will again make the changes with reference to your local time, that is, GMT -6.00.
The Date object stores your date as "2014-02-03T00:00:00.000Z".
When you display your Date object, the toString() function is used to get a string to display the date. toString() displays the date using the local time zone. Try using the toISOString() function or toUTCDateString().