How to get current date with time in js?

Asked

Viewed 2,275 times

3

How to get current date with time in js?

I need to know how many days you ran after a certain date. I’m using momentjs

var start = moment( "2016/06/17" );
var end = moment( new Date() );
var time = date_start.to(date_end);
console.log( moment.duration(time, 'days').humanize());

My problem is that for the same day, I need the time difference of the date. Only the time is reset, so the difference is equal to zero.

1 answer

4


vc can use the momentjs diff function. And when creating a new Moment() you are already creating it with the current date.

[EDITED]

If you want the text to appear in English just add the library en.js and put as first command of js Moment.locale('en-BR');

moment.locale("pt-BR");
var start = moment("2016-06-17T09:00:00");
var end = moment();

console.log(start);
console.log(end);
console.log(moment.duration(end.diff(start,true)).humanize());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.13.0/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.6/locale/pt-br.js"></script>

  • It worked perfectly. Thank you!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.