How to produce a similar new Date() object using moment.js?
I have input as UTC date and output should be similar to new Date() object.
Please help to resolve this.
Input: 1389033000000
Output: Tue Jan 07 2014 00:00:00 GMT+0530 (India Standard Time)
You can use moment().format(pattern), where pattern is a string describing the format you want.
Something like this should work:
const input = 1389033000000
const output = moment(input).format("ddd MMM DD YYYY hh:mm:ss ZZ")
console.log(output)
// Mon Jan 06 2014 04:30:00 GMT-0200
console.log(new Date(input))
// Mon Jan 06 2014 16:30:00 GMT-0200 (Horário de Verão de Brasília)
Furthermore, you can convert a moment to native date with moment().toDate()
const input = 1389033000000
const output = moment(input).toDate()
console.log(output)
// Mon Jan 06 2014 16:30:00 GMT-0200 (Horário de Verão de Brasília)
Use Moment Timezone to convert time to particular timezone
var moment = require('moment-timezone');
var timestamp = 1389033000000;
var usingTimezone = moment(timestamp).tz("Asia/Kolkata").format("ddd MMM DD YYYY hh:mm:ss Z");
var usingOffset = moment(timestamp).utcOffset("+05:30").format("ddd MMM DD YYYY hh:mm:ss Z");
console.log(usingTimezone);
console.log(usingOffset);
Related
I want to convert a string field to timestamp with Impala.
I tried using this expression which works fine in Hive:
select from_unixtime(unix_timestamp('Fri Mar 02 00:00:00 GMT 2018','EEE MMM dd HH:mm:ss z yyyy')) AS Date_Call_Year;
but I get the error "Bad date/time conversion format: EEE MMM dd HH:mm:ss z yyyy". What is wrong with this format?
I want to get the epoch seconds using momentjs.
For example, time right now is 2019-04-20T15:07:04.388Z EST, I want to get yesterday start time epoch 2019-04-19T00:00:00.000Z in UTC.
I have tried the below code -
const now = new Date();
const start = moment(now) // get current datetime
.utcOffset(0) // convert to UTC
.subtract(24, "hours") // go 24 hours into the past
.startOf("day") // get the start of the date we landed on
.unix(); // get unix timestamp
console.log(now);
console.log(start);
The output of the above program is -
Sat Apr 20 2019 15:11:23 GMT-0400 (EDT) {}
1555650000
According to https://www.unixtimestamp.com/index.php the 1555650000 translates to Fri, 19 Apr 2019 05:00:00 +0000. But I want it to be Fri, 19 Apr 2019 00:00:00 +0000 in UTC.
The momentjs version used in our code -
"moment": "2.24.0",
"moment-timezone": "^0.5.23"
Any idea how can I get this?
you can try this one line statement
console.log( moment.utc().subtract(1, 'days').startOf('day').toString());
Output : Sun Apr 21 2019 00:00:00 GMT+0000
is there any way to convert date string Tue Feb 23 2016 20:11:42 GMT+0200 (EET) to timestamp in nodejs?
I was trying to use moment.js with this:
moment('Tue Feb 23 2016 20:11:42 GMT+0200 (EET)', 'YYYY-MM-DD HH:mm:ss').valueOf()
but it returned NaN to me. Maybe its impossible?
There's no need for moment.js
var d = new Date("Tue Feb 23 2016 20:11:42 GMT+0200 (EET)");
var timeStamp = d.getTime();
Another way to do it with moment, but a little unnecessary is:
moment(Date.parse('Tue Feb 23 2016 20:11:42 GMT+0200 (EET)')).format('YYYY-MM-DD HH:mm:ss');
change format with any other format you may desire.
I have date object like this "Tue Sep 02 2014 13:34:17 GMT+0500 (Pakistan Standard Time)"
I want increment a day in the date object using node.js.
I.E.
var myDate = "Tue Sep 02 2014 13:34:17 GMT+0500 (Pakistan Standard Time)";
please mention how to increment a day in above myDate.
Thanks
Try this
var myDate = new Date("Tue Sep 02 2014 13:34:17 GMT+0500 (Pakistan Standard Time)");
myDate.setDate(myDate.getDate() + 1);
alert(myDate);
I've got a date string as such:
Tue Jul 29 2014 23:44:06 GMT+0000 (UTC)
How can I add two hours to this?
So I get:
Tue Jul 29 2014 01:44:06 GMT+0000 (UTC)
Here's one solution:
var date = new Date('Tue Jul 29 2014 23:44:06 GMT+0000 (UTC)').getTime();
date += (2 * 60 * 60 * 1000);
console.log(new Date(date).toUTCString());
// displays: Wed, 30 Jul 2014 01:44:06 GMT
Obviously once you have the (new) date object, you can format the output to your liking if the native Date functions do not give you what you need.
Using MomentJS:
var moment = require('moment');
var date1 = moment("Tue Jul 29 2014 23:44:06 GMT+0000 (UTC)");
//sets an internal flag on the moment object.
date1.utc();
console.log(date1.format("ddd MMM DD YYYY HH:mm:ss [GMT]ZZ (UTC)"));
//adds 2 hours
date1.add(2, 'h');
console.log(date1.format("ddd MMM DD YYYY HH:mm:ss [GMT]ZZ (UTC)"));
Prints out the following:
Tue Jul 29 2014 23:44:06 GMT+0000 (UTC)
Wed Jul 30 2014 01:44:06 GMT+0000 (UTC)
This works well:
const epoch = new Date('01-01-2000')
const notBeforeDate = new Date(epoch).setSeconds(notBefore)
const notAfterDate = new Date(epoch).setSeconds(notAfter)
NOTE: the setSeconds() call actually adds seconds to the current Date value, it does not reset the Date to some absolute number of seconds. This detail is poorly addressed in the documentation and causes a lot of heartache when first trying to work with Dates in JavaScript.