Converting Moment utc time into unix time - javascript

Hello guys I was wondering how do I convert a date into Unix time stamp using the library moment.js so I can compare the oldDate with another date.
This is what I tried:
var oldDate = (moment.unix(1490632174)).format();
// here I got the Date in string format
var newDate= moment.utc('2017-03-27T18:29:59+02:00', "YYYY-MM-DD");
// now I want to convert it again into unix timestamp and I don't know how to do it.
console.log(oldDate, newDate);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>

Documentation is a wonderful thing. Unix Timestamp (seconds)
moment().unix();
moment#unix outputs a Unix timestamp (the number of seconds since the Unix Epoch).
moment(1318874398806).unix(); // 1318874398
This value is floored to the nearest second, and does not include a milliseconds component.
var oldDate = moment.unix(1490632174).unix();
// here I got the Date in string format
var newDate= moment.utc('2017-03-27T18:29:59+02:00', "YYYY-MM-DD");
// now I want to convert it again into unix timestamp and I don't know how to do it.
console.log(oldDate, newDate.unix());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>

You can use the < and > operators:
var oldDate = moment.unix(1490632174);
var newDate= moment.utc('2017-03-27T18:29:59+02:00', "YYYY-MM-DD");
console.log(oldDate<newDate, oldDate>newDate);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>

Another way you could approach this is to leverage the native Date.parse("2017-03-27T18:29:59+02:00 GMT") method in Javascript. This method parses a date string and returns Unix Time in ms.
For more info, checkout Date.parse() documentation and this Unix Time Converter that makes use of both moment and the native JS method.

Related

how to convert the date 22/04/2020 to Epoch timestamp using moment javascript

var date = '22/04/2020';
console.log(moment(date).format("X"));
log coming as NaN. Please provide a solution for, required output is 1587568445
Try using moment's unix() method. This would give epoch equivalent of "22/04/2020".
Here the time pattern has been removed, hence it results 1587340800.
For your output, pass in 22/04/2020 20:44:05 to get 1587568445 which is the epoch equivalent
var epoch = moment("22/04/2020", "DD/MM/YYYY").unix();
console.log(epoch);
var epoch1 = moment("22/04/2020 20:44:05", "DD/MM/YYYY hh:mm:ss").unix();
console.log(epoch1);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>

How can the Moment.js get the timestamp?

How can I get the timestamp after manipulating days in Moment.js?
I tried use:
var a = moment().subtract(10, 'days').calendar().getTime()
to get the timestamp, but failed.
It is not really clear what you mean by "timestamp".
To get the number of milliseconds since the Unix epoch, use moment().valueOf();. This corresponds to JS Date.getTime();.
To get the number of seconds since the Unix epoch, use moment().unix();.
To get the hour/minute/second as numbers, use moment().hour(); / moment().minute(); / moment().second();.
To get the ISO 8601 string (recommended for sending data over the wire), use moment().toISOString(); (e.g. "2013-02-04T22:44:30.652Z").
To print a user readable time, use moment().format();, e.g. moment().format('LTS') will return the localized time including seconds ("8:30:25 PM" for en-Us).
See moment.js - Display - Format for format specifiers.
Classic calendar formatting:
const daysBefore = moment().subtract(10, 'days').calendar();
for unix timestamp (current date minus 10 days):
const daysBefore = moment().subtract(10, 'days').unix();
Just remember, apply formatting after subtraction, as formatting returns a string, not momentjs object.
You can use moment().toDate() to get a Javascript Date object. Then you can get the timestamp with getTime().
calendar does not have method getTime(). You can format your time as follows:
let format = 'YYYYMMDD';
let a = moment().subtract(10, 'days').format(format);
console.log(a)
Try the following to get the timestamp:
moment().subtract(10, 'days')._d
try this one
moment().subtract(10, 'days').calendar().format('HH:MM:SS'); //get datetime
moment().subtract(1, 'days').calendar(); // Yesterday at 1:16 PM

Why does Date.UTC gives back a string of milliseconds

UTC is of format - 1994-11-05T08:15:30-05:00
But why does Date.UTC gives a string representing milliseconds ?
How can I achieve the same in Javascript and what are the browser compatibility cases I need to take care of while doing without external library ( say moment ).
Date.UTC returns the date in the UTC/GMT timezone, as a Unix timestamp.
What you are referring to as UTC format is ISO-8601 format, and you retrieve that with the toISOString() method. For example:
var d = new Date();
var n = d.toISOString();

moment fromNow returns in 5 hours when parsing utc

trying to format utc time from server in time ago using moment.js fromNow but in some occasions I get "in 5 hours" instead.
timestamp from a server - 2017-11-29T15:03:21
var utcTime = new Date(timestamp);
var timeAgo = moment(utcTime).fromNow();
console.log(timeAgo)
all dates are in past so how can I fix this so I dont get time in a few hours ?
If you want "2017-11-29T15:03:21" treated as UTC, you can either use moment's utc method or just append a "Z" to the string. Since you're already using moment.js, it's more reliable to parse it with moment.js than the built-in parser:
var timestamp = "2017-11-30T00:20:48";
// Append Z
console.log(moment(timestamp + 'Z').fromNow());
// Use .utc
console.log(moment.utc(timestamp).fromNow());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.19.3/moment.min.js"></script>
You need to tell moment that this date is in UTC using moment.utc
var utcTime = new Date(timestamp);
var timeAgo = moment.utc(utcTime).fromNow();
If you don't, moment assumes this date is in your local timezone (which I can tell is Eastern Standard Time by the offset).
In your local timezone, this date is actually 5 hours in the future. Only in UTC is it a few seconds ago, because your local timezone is 5 hours behind UTC.
As per documents https://momentjs.com/docs/#/displaying/fromnow/
you can customize the locale https://momentjs.com/docs/#/customization/relative-time/
As default locale future time will be future: "in %s", having in which is as per documents. if you want to change it then update the locale and use as you want.
Hope this helps

How to substract current utc time from php iso utc?

I want to get current time in UTC, substract it from ISO UTC string which I'm getting from backend in the following format: 2016-11-29T17:53:29+0000 (ISO 8601 date, i guess?) and get an answer in milliseconds.
What is the shortest way to do it in javascript? I'm using angular 2 and typescript frontend.
You can get the current time using:
var now = Date.now()
And convert the string to a milliseconds timestamp using:
var ts = (new Date('2016-11-29T17:53:29+0000')).getTime()
And then subtract the values:
var diff = ts - now

Categories