Generate and format dates with Moment on Nodejs

1

I'm trying to use Moment to format dates in this way YYYY-MM-DD HH:mm:ss and would like to determine a time period between 00:00:00 p.m. yesterday and 23:59:59 today. Example: Considering today as 07/25/2017, it would look like:

dateFrom: 2017-07-24 00:00:00
dateTo:  2017-07-25 23:59:59

How do I generate those dates with Moment and force the formatting into YYYY-MM-DD HH:mm:ss ?

    
asked by anonymous 25.07.2017 / 22:46

1 answer

0

Given a variable "data1" that is an object of type Date already has the correct date in question, which in this case is 25/07/2017 , just do the following:

// data1 já é um objeto do tipo "Date"
var dt = moment(data1);
// 
console.log(dt.format('YYYY-MM-DD 00:00:00'));
console.log(dt.format('YYYY-MM-DD 23:59:59'));

// Se "data1" também tiver algum valor de hora setado que quisermos saber
console.log(dt.format('YYYY-MM-DD HH:mm:ss'));
    
30.07.2017 / 05:14