Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert Date to timestamp using MomentJS?

I used MomentJS to convert local date to UTC date using the following way:

$("#div1").text(moment("2016-10-11 18:06:03").tz("Europe/Paris").format());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.14.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.6/moment-timezone-with-data.min.js"></script>

<div id="div1"></div>

Now I need timestamp from the output value using MomentJS.

like image 225
Asmi Avatar asked Oct 20 '16 06:10

Asmi


People also ask

Is MomentJS dead?

We now generally consider Moment to be a legacy project in maintenance mode. It is not dead, but it is indeed done.

Is MomentJS deprecated?

Moment construction falls back to js Date. This is discouraged and will be removed in an upcoming major release. This deprecation warning is thrown when no known format is found for a date passed into the string constructor.


5 Answers

moment().format("X"); // lowercase 'x' for milliseconds

var date = moment('2016-10-11 18:06:03').tz('Europe/Paris').format(),
    timestamp = moment(date).format("X");

$('#div1').text(date);
$('#timestamp').text(timestamp);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.21/moment-timezone-with-data.min.js"></script>

<div id="div1"></div>
<div id="timestamp"></div>
like image 164
Nhan Avatar answered Oct 10 '22 15:10

Nhan


You said:

I used MomentJS to convert local date to UTC date using the following way: moment("2016-10-11 18:06:03").tz("Europe/Paris").format()

That doesn't do that. That converts a local value to Paris time, and emits it as a string in ISO8601 format.

Now I need timestamp from the output value using MomentJS.

That's a different question, and wouldn't involve the output of the above because:

  1. You can't get a timestamp from the output string, you'd get it from a moment object. You could parse that string, but that would be silly since you already had a moment object earlier.

  2. Timestamps are UTC based, so time zone conversion is irrelevant. You'd get the same timestamp if you didn't convert at all.

You can get a string with a timestamp using .format('X') or .format('x') depending on which precision you want. But it's much cleaner to just get the numerical timestamp using .valueOf() or .unix(), again depending on precision.

like image 36
Matt Johnson-Pint Avatar answered Oct 10 '22 15:10

Matt Johnson-Pint


To get the time from the date use the format method of momentjs

var date = moment('2016-10-11 18:06:03').tz('Europe/Paris').format();
 

console.log(date);
console.log(moment(date).format("X"));
console.log(moment(date).format("x")); // for milliseconds
console.log(moment(date).format("HH:mm"));
console.log(moment(date).format("hh:mm A"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.21/moment-timezone-with-data.min.js"></script>
like image 42
Deepu Reghunath Avatar answered Oct 10 '22 15:10

Deepu Reghunath


Just to expand on something mentioned earlier. These generate the results.

// less precision
moment().unix() === moment().format('X')  // uppercase X

// more precision
moment().valueOf() === moment().format('x')  // lowercase x

Hope that sheds some light

like image 20
Jose Perez Avatar answered Oct 10 '22 14:10

Jose Perez


I think moment().unix() do the trick ! See https://momentjs.com/docs/#/displaying/unix-timestamp-milliseconds/

like image 23
Atef Ben Ali Avatar answered Oct 10 '22 14:10

Atef Ben Ali