This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Convert a Unix timestamp to time in Javascript
I'm trying to convert the string that this api returns for date. The format is 1351993013. I tried the following line of JS but the date is completely wrong.
var jsonDate = plugin.versions[0].date;
var pluginDate = new Date(jsonDate);
Which returns:
Fri Jan 16 1970
This is the first time I've tried to format a JSON date so it's a bit confusing. Can anyone help?
That would be seconds, and javascript uses milliseconds which would be
1351993013000
which would give you Sunday Nov 04 2012.
in other words:
var jsonDate = parseInt(plugin.versions[0].date, 10) * 1000;
Related
This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Closed 2 years ago.
I get date_time from mssql db.
Currently date come like that 2021-01-30T15:08:25.393Z
When I use this method;
const date =date;
var d = new Date(date).toUTCString()
My date is looking like Sat, 30 Jan 2021 15:08:25 GMT
But, ı want to see date "dd/mm/yy hh:mm:ss"
How should I do this ?
As I've checked in my Chrome tools JS you can mix up this methods:
const date = new Date('2021-01-30T15:08:25.393Z').toLocaleDateString('us')
const hours = new Date('2021-01-30T15:08:25.393Z').toLocaleTimeString('us')
console.log(`${date} ${hours}`)
and concat that results
Edited. I've found that
new Date('2021-01-30T15:08:25.393Z').toLocaleDateString('fr')
is way similar to what date format you need
This question already has answers here:
Convert UNIX timestamp to date time (javascript)
(4 answers)
Closed 2 years ago.
I am using openweather map api for creating a weather application. The response data for daily has the following format:
{"dt":1590343200,"temp":302.72,..,..,}
It also provides timezone offset.
timezone_offset":19800
How do I calculate current date from the above 2 values using javascript ?
Thanks
Looks like a unix timestamp so:
const d = new Date((1590343200 - 19800) * 1000)
d.toGMTString() // "Sun, 24 May 2020 12:30:00 GMT"
This question already has answers here:
Why does Date.parse give incorrect results?
(11 answers)
Parsing a string to a date in JavaScript
(35 answers)
Closed 2 years ago.
I am trying to convert this date into Mon Nov 26 2018 10:32:04 GMT (I am getting this data from the Api so i can't make changes to it)
I assume it is considering 26 as months thats why it is showing it as invalid date
Can Anyone help me with this. How to convert that date into the expected output i specified.
How to get
var d = new Date("26-11-2018 10:32:04")
return d; //Error: Invalid Date
expected Output: Mon Nov 26 2018 10:32:04 (IST)
Use moment.js to parse the date.
moment("26-11-2018 10:32:04", "DD-MM-YYYY HH-mm-ss").toDate()
Alternatively, if you really don't want to use moment for whatever reason, you can use regex magic.
new Date("26-11-2018 10:32:04".replace(/^(\d+)-(\d+)-(\d+) (\d+):(\d+):(\d+)$/, "$3-$2-$1T$4:$5:$6Z"))
This is not a robust as #Yevgen answer but it also much simpler.
All I'm doing is removing the - and flipping the day and month values
const items = "26-11-2018 10:32:04".split('-')
new Date(`${items[1]} ${items[0]} ${items[2]}`)
This works for personal projects but I highly recommend using moment.js
This question already has answers here:
Convert iso timestamp to date format with Javascript?
(4 answers)
Closed 7 years ago.
I have date in ISO-format like:
2016-02-17T16:40:30
How can I convert it to a human-readable date, for example:
17 Feb 2016 16:40
First of all, you need to create a date using your original date string.
var d = new Date('2016-02-17T16:40:30');
And then you can use this to fetch a readable date format:
d.toDateString();
Will return:
Wed Feb 17 2016
This question already has answers here:
Javascript JSON Date parse in IE7/IE8 returns NaN
(5 answers)
Closed 7 years ago.
Chrome showing result as expected but IE-8 giving NAN when i execute the following:
Chrome:
d = new Date("2014 12 01") // results Mon Dec 01 2014 00:00:00 GMT+0500 (Pakistan Standard Time)
IE-8:
d = new Date("2014 12 01") // results NaN undefined
The format you're trying to parse doesn't match the only specific format that new Date is required to parse. To parse it reliably cross-browser, you need to parse it explicitly — either in your own code, which can be trivially done with a regex, or using a library like MomentJS and telling it what the format is.
The trivial regex solution:
// NOTE! Uses local time.
var yourString = "2014 12 01";
var parts = yourString.match(/^(\d{4}) (\d{2}) (\d{2})$/);
if (parts) {
var date = new Date(+parts[1], +parts[2] - 1, +parts[3]);
alert(date.toString());
}