How to convert epoch time to ISO 8601 using moment.js [duplicate] - javascript

This question already has an answer here:
Moment returns invalid data even though date is correct
(1 answer)
Closed 4 years ago.
I have epoch time in array "task[i]["due_on"]=1533541797" which I am trying to convert to ISO 8601 using 'moment'. I have also tried in different ways but it always shows a different error.
var moment = require('moment');
var dueon_date = moment(task[i]["due_on"]).format();
console.log("date",dueon_date);
console logs output - Invalid date

Just specify the input format:
var dueon_date = moment("1533541797", "X").format();
console.log(dueon_date);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.js"></script>

Related

date-fns parse for "/Date(1633421520000)/" [duplicate]

This question already has answers here:
How do I format a Microsoft JSON date?
(42 answers)
Closed 1 year ago.
receiving string of date that looks like this "/Date(1633421520000)/",
with moment I could just use it as moment("/Date(1633421520000)/") whats the equivalent for date-fns ?
for example differenceInMilliseconds how would i use it with received argument as this string "/Date(1633421520000)/"
not sure how to create my date object from this string so ill be able to use date-fns functions.
You need to extract the number (which looks like milliseconds since unix epoch) from the string:
"/Date(1634717139973)/".match(/\d+/)[0]
Then use Date constructor like so:
var date = new Date(Number("/Date(1634717139973)/".match(/\d+/)[0]));
date.toISOString(); // 2021-10-20T08:05:39.973Z

JS formatting date ymd to yyyy-mm-dd [duplicate]

This question already has answers here:
Moment.js - How to convert date string into date?
(3 answers)
Closed 3 years ago.
I am getting my date from PHP in format ymd. I am using Moment.js to convert it to format I need, but it's not working properly.
So for example today's date is comes like 190528 but after conversion to YYYY-MM-DD it becomes 190528-1-1 instead of 2019-05-28.
Is it the first format of the date which causes the problem, or there is a way to overcome this and convert it the way I need?
Parse Date first by passing format as 'YYMMDD'
moment("190528", "YYMMDD").format("YYYY-MM-DD");
This will give proper output.
You do not need to load moment for this.
You can break the string into parts of years, month and date. Format the year and return the joined string...
function formatDate(date) {
var _d = date.match(/.{1,2}/g);
_d[0] = "20" + _d[0];
return _d.join("-");
}
var d = "190528";
console.log(formatDate(d));
You need to tell moment what format your date string is in:
console.log(moment("190528", "YYMMDD"))
<script src="https://unpkg.com/moment#2.24.0/moment.js"></script>
You can pass the format of current date as a second param of moment.
console.log(moment("190528", "YYMMDD").format("YYYY-MM-DD"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
Here the docs.

How to handle JWT.exp time? [duplicate]

This question already has answers here:
Convert a Unix timestamp to time in JavaScript
(34 answers)
Closed 4 years ago.
console.log('DEBUG::+jwtDecode(token).exp', +jwtDecode(token).exp); //1534820211
console.log('DEBUG::try', new Date(+jwtDecode(token).exp).toISOString());
//DEBUG::try 1970-01-18T18:20:20.211Z
I'm having a token with value 1534820211 and when I try to convert it using toISOString() it gives me year 1970-01-18T18:20:20.211Z.
But when I decode the same token at jwt.io, and mouse hover over exp, it shows 2018-08-21.... which is huge difference. I have also tried to pass jwtDecode(token).exp into moment and using format, still return me datetime in 1970xxxx.
moment(jwtDecode(token).exp).format();
The value you have is seconds from epoch.
JavaScript Date constructor (and moment function too) accepts value in milliseconds from epoch. Multiply the number by 1000, and your code should work fine:
var exp = 1534820211 * 1000;
console.log(new Date(exp));

angular-bootstrap date picker shows incorrect date for 'yyyy-mm-dd' format [duplicate]

This question already has answers here:
javascript Date timezone issue
(3 answers)
javascript doesn't convert angular ui datepicker date to UTC correctly
(3 answers)
Closed 7 years ago.
We're storing birth dates in the format of yyyy-mm-dd. When this format is provided to the angular-bootstrap date picker, it selects the incorrect date in the popup. Converting it to a date object causes both the display and selection to be incorrect. See my plnk for examples (ignore validation stuff, that's a whole other issue).
// Displays '2015-09-25', but 24th is selected
var date = '2015-09-25';
// Displays '2015-09-24', selects 24th
var date = new Date("2015-09-25");
From javascript Date timezone issue: "In JavaScript, a value in the format of YYYY-MM-DD is interpreted as a UTC value, rather than a local-time value."
One workaround is to replace the hyphens with slashes:
var s = "2015-09-25";
var dt = new Date(s.replace(/-/g, '/'));
I would recommend using moment.js though. It works for me and I had the same problem with a javascript datepicker.
var s = "2015-09-25";
var dt = moment(s, 'YYYY-MM-DD').toDate();

can't format javascript timestamp string toLocaleString [duplicate]

This question already has answers here:
Problem with date formats in JavaScript with different browsers
(4 answers)
Closed 7 years ago.
new Date().toLocaleString() --> "‎24‎/‎09‎/‎2015‎ ‎10‎:‎14‎:‎00‎ ‎PM"
new Date("2015-09-24 09:38:32.639").toLocaleString() --> "Invalid Date"
How can I format a date object from a timestamp in string format?
SOLUTION: At the end I got it fixed changing my date type in the server from DateTime to Instant, js will atomatically add zone offset automatically from a timestamp and will format the dates in the right way.
NOTE: I know this question is duplicated, however the solution proposed is different and may help other users to get a different approach to their code.
var myDate = "2015-09-24 09:38:32.639";
new Date(myDate.replace(/-/g,"/")).toLocaleString()
Now it's working fine

Categories