Maclean Pinto
Maclean Pinto

Reputation: 1135

Get start of a given date using momentjs

I am trying to get the start of and end of day. Below is my code.

var m = moment(new Date('Fri Aug 8 2017 11:31:08 GMT+0530 (India Standard Time)')).startOf('day')
    
console.log(m);    
    
var n = moment(new Date('Fri Aug 8 2017 11:31:08 GMT+0530 (India Standard Time)')).endOf('day')

console.log(n.format()); 
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>

I even tried

moment(new Date('Fri Aug 8 2017 11:31:08 GMT+0530 (India Standard Time)')).startOf('date')

Nothing seems to work. Does moment support this functionality.

Upvotes: 2

Views: 7096

Answers (1)

Shubham Khatri
Shubham Khatri

Reputation: 282160

It does work with the latest version of moment. Check the snippet.

Also if you want to format the time in IST you can apply a fixed UTC offset and format the date in the desired format.

var st = moment(new Date('Fri Aug 8 2017 11:31:08 GMT+0530 (India Standard Time)')).startOf('day').utcOffset("+05:30").format()
console.log(st);

var end = moment(new Date('Fri Aug 8 2017 11:31:08 GMT+0530 (India Standard Time)')).endOf('day').utcOffset("+05:30").format()
console.log(end);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>

Upvotes: 4

Related Questions