Convert yyyymmddThhmmss.SSSZ format to Unix Timestmp - node.js

I'm using NodeJS to fetch a date from a server, but the date format is yyyymmddThhmmss.SSSZ
20170423T203146.000Z
I'm trying to convert this date string into epoch time, to easily calculate a difference between this time and the current time. (the timezone will always be UTC)
However, I could not find any possibility to parse this string, since libraries don't seem to accept this kind of date string.
Can someone help me out with this?

Moment.js seems to give me correct parsed date
var moment = require('moment')
var date = moment("20170423T203146.052Z" , "YYYYMMDDThhmmss.SSS")
console.log(date.format("YYYY MM DD hh mm ss SSS"))
Output: 2017 04 23 08 31 46 052

you should take a look at Moment.js String+Format + Special Format
It seems to work very well on your time-string already:
const moment = require("moment");
const werner = "20170423T203146.000Z";
console.log(moment.utc(werner).format());
const epochSec = moment.utc(werner).unix();
const epochMs = moment.utc(werner).valueOf();
console.log(epochSec, epochMs);
Viel Erfolg ;)

Related

Moment Timezone JS return true date conversion on development but return incorrect in production

I made a time conversion between original time 'Asia/Jakarta' (GMT+7) and 'Asia/Tokyo' (JST). The actual difference is 2 hours (GMT+7 00:00, JST 02:00) In development, it return with true value like this:
24 Januari 2020, 00:00 GMT+7
24 Januari 2020, 02:00 JST
But when it uploaded to production server (the server was placed at NY, US), the conversion was incorrect. Returning
24 Januari 2020, 00:00 GMT+7
24 Januari 2020, 09:00 JST
I thought that I need to set Default Timezone, but the error was getting worse. And got
25 Januari 2020, 07:00 GMT+7
25 Januari 2020, 09:00 JST
Here's the code that I write:
// calling the modules
const moment = require('moment');
const momentTz = require('moment-timezone');
//... initializing code
moment.locale('id');
moment.tz.setDefault('Asia/Jakarta');
// args[1] = '24/01'
// args[2] = '00:00'
try {
const timeFormat = 'Do MMMM YYYY, HH:mm';
const dateSplit = args[1].split('/');
const date =
dateSplit[1] + '/' + dateSplit[0] + '/' + moment().format('YYYY');
const dateTime = Date.parse(`${date} ${args[2]}`);
const livestreamDateTime = moment(dateTime).format(timeFormat);
const livestreamDateTimeJapan = momentTz
.tz(dateTime, 'Asia/Tokyo')
.format(timeFormat);
const vliverFirstName = args[0].toLowerCase();
const vData = vliver[vliverFirstName];
const youtubeId = args[3];
// ...
// ... showing the output and catching error underneath
The date I got from typing user input. So when user type 24/01 00:00, it says that the date is 24 January 2020 00:00 GMT+7
EDIT: I've checked the production server date. It shows my GMT+7 Timezone.
Fixed the issue.
Because the time difference is actually 2 hours. And what I need to do is comparing 2 Time zones only, so what I need to do is to add the input date and time by 2 hours. And also not set the Default Time zone
const date = dateSplit[1] + '/' + dateSplit[0] + '/' + moment().format('YYYY');
const dateTime = Date.parse(`${date} ${args[2]}`);
const livestreamDateTime = moment(dateTime).format(timeFormat);
const livestreamDateTimeJapan = moment(dateTime)
.add(2, 'hours')
.format(timeFormat);
It still returns as same timezone. But I think that's not a big deal because I hide the timezone and replace it with hard coded string.

convert string to ISO date time in nodejs

Convert this date into ISO format in nodejs
created_at="September 17th 2019, 16:50:17.000";
let new_time = new Date(created_at);
created_at = new_time.toISOString();
console.log(created_at);
Output: Invalid Date
Exacting output is in ISO format. like this 2011-10-05T14:48:00.000Z
Moment.js is a library which you can use to get the output and do some advanced operations with date and timezones.
Below is the code to get expected output.
var moment = require('moment')
created_at="September 17th 2019, 16:50:17.000";
let new_time = moment("September 17th 2019, 16:50:17.000", "MMMM Do YYYY, HH:mm:ss:SSS");
created_at = new_time.toISOString();
console.log(created_at);
You will have to pass the date string in below format in order to convert it to ISO date:
var date1 = "September 17 2019, 16:50:17.000";
let new_date = new Date(date1);
console.log(new_date);
console.log(new_date.toISOString());

Momentjs get time in current location

I'm trying to generate a momentjs object of a certain timestamp in the current day of a specified location. For example:
const timeNow = moment().tz('Africa/Cairo')
const startTime = moment('10:00 am', 'HH:mm a')
const endTime = moment('2:30 pm', 'HH:mm a')
Printing the above 3 variables outputs this:
Fri, 12:31 am
Thu, 10:00 am
Thu, 02:30 pm
Where the first result is in fact the current time in Cairo, However the other two results are the day before. How can I change it so that they return the current day?
You can simply do:
moment.tz('Africa/Cairo') // <= Moment Object
One small info: whenever you'll get to see some javascript date in a browser that will be shown in your system's time-zone. As javascript Date is UTC, browsers will show accordingly. Use moment.format() to get string values.

Format the time from moment js

When I am trying to receive mail from gmail, I get time in this format (Mon, 12 Jun 2017 10:29:07 +0530). I want to calculate time minus current time. How to do so?
var testDate = moment("Mon, 12 Jun 2017 10:29:07 +0530");
//Relative to time in human readble format
testDate.fromNow(); //3 days ago
You can simply call the moment constructor with the given Date format. moment.js is smart enough to parse it for you. To get the difference you can convert it into unix based time format and subtract it.
const givenTime = moment("Mon, 12 Jun 2017 10:29:07 +0530").unix()
const currentTime = moment().unix()
//Difference in milliseconds
const diff = givenTime - currentTime

momentjs wrong date returned

Im using momentjs in nodejs app and trying to parse timestamp string with momentjs to compare two dates. My code is:
moment('11/04/2016 01:00:00', 'DD/MM/YYYY HH:mm:ss')
and the output is:
{ _date: Fri Nov 04 2016 00:00:00 GMT+0000 (GMT Standard Time) }
But with the given format im expecting the return date to be Sun Apr 10 2016.
Seems like the format option is not working or im missing something
It works for me (moment version: 2.12.0)
var moment = require('moment')
var m = moment('11/04/2016 01:00:00', 'DD/MM/YYYY HH:mm:ss')
console.log(m.format('LLL')) // April 11, 2016 1:00 AM
Also changing days with months in both arguments works:
var m = moment('04/11/2016 01:00:00', 'MM/DD/YYYY HH:mm:ss') // same date as above
What version of moment are you using and how did you import it? How did you generate the output date? Is it just console.log?
(this should be a comment, but I wanted to add the code snippet)
Here's a sample code to return a string in your output format
var moment = require('moment');
var now = moment();
var date = moment('11/04/2016 01:00:00', 'DD/MM/YYYY HH:mm:ss');
console.log(now.format('ddd MMM D YYYY'));
console.log(date.format('ddd MMM D YYYY'));
Sorry it was my mistake. I've installed momentjs which is not the official moment package. After installing the moment package instead momentjs all is working as expected.

Resources