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
Related
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 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'm trying to convert a date string into a date object without changing the timezone. Here is the standard behavior:
new Date ("2014-10-24T00:00:00")
result
Thu Oct 23 2014 19:00:00 GMT-0500 (Central Daylight Time)
I am able to reverse the timezone by getting the offset in minutes, multiplying it by 60,000, and then adding that to the new string date.
new Date(new Date("2014-10-24T00:00:00").getTime() + new Date().getTimezoneOffset()*60000)
This works, but it seems like there must be a better way that doesn't require created three date objects.
Do not parse strings using the Date constructor. It calls Date.parse which, despite being standardised for one version of ISO 8601 strings in ES5, is still almost entirely implementation dependent.
I'm trying to convert a date string into a date object without changing the timezone.
> new Date ("2014-10-24T00:00:00")
That string will be treated differently in different browsers. If you want it to be treated as UTC, then it is simple to parse yourself:
function parseISOAsUTC(s) {
var b = s.split(/\D/);
return new Date(Date.UTC(b[0],--b[1],b[2],b[3],b[4],b[5],(b[6]||0)));
}
console.log(parseISOAsUTC('2014-10-24T00:00:00').toISOString()); // 2014-10-24T00:00:00.000Z
Now you can be certain that the string will be treated as UTC in all browsers in use (including the 20% or so still using IE 8 and lower).
If, on the other hand, you want the string to be treated as a local time, then just remove the Date.UTC part:
function parseISOAsLocal(s) {
var b = s.split(/\D/);
return new Date(b[0],--b[1],b[2],b[3],b[4],b[5],(b[6]||0));
}
console.log(parseISOAsLocal('2014-10-24T00:00:00')); // Fri 24 Oct 2014 00:00:00 <local timezone>
Here is an implementation of zerkms's solution.
new Date("2014-10-24T00:00:00".replace('T', ' '))
result
Fri Oct 24 2014 00:00:00 GMT-0500 (Central Daylight Time)
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
I have a timestamp 1378028575 that gives me Sun, 01 Sep 2013 09:42:55 GMT here. But when I try to format it with Angular date, it returns it as Jan 17, 1970 2:47:08 AM, using this format: {{'1378028575' | date:'medium'}}. The result from the site is correct but in Angular is wrong. Why does it happen, or what am I doing wrong?
Its cause you use seconds not milliseconds.
new Date(1378028575)
Fri Jan 16 1970 23:47:08 GMT+0100 (CET)
new Date(1378028575000)
Sun Sep 01 2013 11:42:55 GMT+0200 (CEST)
from the angular docs:
Date to format either as Date object, milliseconds (string or number)
or various ISO 8601 datetime string formats (e.g.
yyyy-MM-ddTHH:mm:ss.SSSZ and its shorter versions like
yyyy-MM-ddTHH:mmZ, yyyy-MM-dd or yyyyMMddTHHmmssZ). If no timezone is
specified in the string input, the time is considered to be in the
local timezone.
The other answer isnt quite complete. Since your timestamp is in seconds, not miliseconds, in Angular.js you can do this:
{{1378028575 * 1000 | date:'medium'}}
Knowing seconds * 1000 = miliseconds is one thing. Knowing you can put math in the date expression is another :)