2017-04-26 6 views
2

Hallo Leute, ich frage mich, wie ich ein Datum in Unix Zeitstempel mit der Bibliothek moment.js konvertieren, so dass ich das OldDate mit einem anderen Datum vergleichen kann.Konvertieren Moment Zeit in Unix Zeit

ist, was ich versucht:

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>

+0

Warum jetzt Moment einzufangen und zweimal benutzen? 'now = moment'' newDate = now.utc (...) '' stamp = now.unix() ' – Octopus

+0

Dein Snippet lief überhaupt nicht, ich habe es für dich repariert. –

Antwort

3

Dokumentation ist eine wunderbare Sache. Unix Timestamp (seconds)

moment() .unix();

moment # unix gibt einen Unix-Zeitstempel aus (die Anzahl der Sekunden seit der Unix-Epoche).

Moment (1318874398806) .unix(); // 1318874398

Dieser Wert ist auf die nächste Sekunde aufgetragen und enthält keine Millisekundenkomponente.

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>

1

können Sie die < und > Operatoren:

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>