Reputation: 1536
I'm simply trying to convert a timestamp back into a human readable date but what I get when converted is strange and wrong.
This is how I save the timestamp:
var timestamp = Number(new Date());
localStorag.setItem("mytimestamp", timestamp);
and this is how I get it back and convert it to readable date:
var mydate = localStorag.getItem("mytimestamp");
var jsDate = new Date(mydate*1000);
alert(jsDate);
The jsDate
is wrong and I don't understand what's causing it!
Could someone please advise on this?
Upvotes: 13
Views: 50394
Reputation: 478
if your date is string and looks like this "2023-11-08T08:49:35.000000Z". You don't need any library for formatting it.
const date = new Date("2023-11-08T08:49:35.000000Z").toDateString();
console.log(date); // "Wed Nov 08 2023"
Upvotes: 1
Reputation: 1623
JS Code
var timestamp = Number(new Date());
localStorage.setItem("mytimestamp", timestamp);
var mydate = localStorage.getItem("mytimestamp");
var jsDate = new Date(Number(mydate));
alert(jsDate);
const months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
const days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
var year = jsDate.getFullYear(),
monthnum = jsDate.getMonth() + 1,
twodigitsmonthnum = ("0" + monthnum).slice(-2),
monthname = months[jsDate.getMonth()],
daymonthnum = jsDate.getDate(),
twodigitsdaymonthnum = ("0" + daymonthnum).slice(-2),
dayname = days[jsDate.getDay()],
hours = jsDate.getHours(),
twodigitshours = ("0" + hours).slice(-2),
minutes = jsDate.getMinutes(),
twodigitsminutes = ("0" + minutes).slice(-2),
seconds = jsDate.getSeconds(),
twodigitsseconds = ("0" + seconds).slice(-2),
milliseconds = jsDate.getMilliseconds();
alert(dayname + ", " + daymonthnum + " " + monthname + " " + year + ", " + twodigitshours + ":" + twodigitsminutes + ":" + twodigitsseconds + "." + milliseconds);
Upvotes: -1
Reputation: 181
Here is an easy solution using the toDateString()
method:
const date = new Date(timestamp).toDateString();
console.log(date);
This will return something like: Thu Jul 14 2016
Upvotes: 18
Reputation: 1528
You can use the Number
data type instead of date * 1000
to achieve this. See code example below:
// generate a timestamp
var timestamp = Number(new Date()) //1479895361931
Then
// get the date representation from the timestamp
var date = new Date(timestamp) // Wed Nov 23 2016 18:03:25 GMT+0800 (WITA)
Upvotes: 19
Reputation: 7565
Try using moment.js
. It add functions such as
moment().format('MMMM Do YYYY, h:mm:ss a'); // November 23rd 2016, 12:03:36 pm
moment().format('dddd'); // Wednesday
moment().format("MMM Do YY"); // Nov 23rd 16
moment().format('YYYY [escaped] YYYY'); // 2016 escaped 2016
moment().format(); // 2016-11-23T12:03:36+02:00
Upvotes: 4
Reputation: 47956
The cause of the issue here is when you multiply the timestamp by 1000.
If you simply pass myDate
to the Date
constructor you should get the correct time -
var timestamp = Number(new Date());
localStorage.setItem("mytimestamp", timestamp);
// ...
var mydateStr = localStorage.getItem("mytimestamp");
var myDate = Number(mydateStr); // convert the string back to a number
var jsDate = new Date(mydate);
It's true that Javascript deals with milliseconds but since you are generating the timestamp with Javascript and then reading it back also with Javascript, no conversion is needed - you can use the value as-is.
As was pointed out to me by @manish in the comments, the value stored in localStorage will be a string - remember to convert it back to a number before passing it to the Date constructor.
Upvotes: 5
Reputation: 97
var yourTimestamp = localStorag.getItem("mytimestamp");
jsDate = new Date(yourTimestamp);
jsDateValues = [
jsDate.getFullYear(),
jsDate.getMonth()+1,
jsDate.getDate(),
jsDate.getHours(),
jsDate.getMinutes(),
jsDate.getSeconds(),
];
alert(jsDateValues); //=> [2011, 3, 25, 23, 0, 0]
and you can use the baove values and print in which format you wnat e.g dd/mm/yyyy
Upvotes: 2
Reputation: 5056
The problem here is that when you store something in localstorage its being stored as string '1479895747557'
and not a number So when you get the timestamp back from localstorage its not a number its a string. Though when you multiply it the value actually changes due to internal conversion it becomes 1479895747557000
and this represents a different date from the one you saved. So the solution would be to convert the string back to a number. Here is the code that works fine.
var timestamp = Number(new Date());
localStorage.setItem("mytimestamp", timestamp);
var mydate = localStorage.getItem("mytimestamp");
var jsDate = new Date(Number(mydate));
alert(jsDate);
Here is the code that does not performs the conversion back to number this will give you an Invalid Date.
var timestamp = Number(new Date());
localStorage.setItem("mytimestamp", timestamp);
var mydate = localStorage.getItem("mytimestamp");
var jsDate = new Date(mydate);
alert(jsDate);
Upvotes: 1