Format the time from moment js - node.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

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.

NodeJs How to use moment-timezone to convert Epoch to Specific TImezone and format

I have a use-case in which I want to convert an Epoch integer value into a specific Timezone time and a specific format,
Also, I want to convert a human-readable date time into epoch.
I am trying to use moment-tz for the timezone conversion.
I using a specific Epoch timestamp 1555296000 which is -
Monday, April 15, 2019 10:40:00 AM in Kuala Lampur Malaysia,
I am able to convert 2019-04-15 10:40:00 of Asia/Kuala_Lumpur timezone into correct Unix.
But I am unable to convert 1555296000 into another timezone's unix,
i.e I wish to convert 1555296000 into equivalent YYYY-MM-DD hh:mm:ss of Asia/Calcutta timezone.
Following is the code I'm trying to work with -
var moment = require('moment-timezone');
console.log("Convert from Asia/Kuala_Lumpur to Unix -> ", moment.tz("2019-04-15 10:40:00","Asia/Kuala_Lumpur").unix());
// Outputs - 1555296000
console.log("Epoch to Specific TimeZone and Format -> ",moment(1555296000).tz("Asia/Calcutta").format('YYYY-MM-DD hh:mm:ss'));
// Outputs - 1970-01-19 05:31:36
// I want - 2019-04-15 08:10:00
Try this out
const moment = require("moment-timezone");
console.log(
moment
.unix(1555296000)
.tz("Asia/Calcutta")
.format("YYYY-MM-DD HH:mm:ss")
);
2019-04-15 08:10:00 - "Asia/Calcutta" is 2019-04-15 10:40:00 - "Asia/Kuala_Lumpur"

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.

Convert yyyymmddThhmmss.SSSZ format to Unix Timestmp

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 ;)

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