2

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.

Maclean Pinto
  • 1,075
  • 2
  • 17
  • 39
  • You need the start day without time? – Ashish Lohia Aug 18 '17 at 06:37
  • 1
    If you are using moment.js you should also use it for parsing date strings, see [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) You should not use the built-in parser. – RobG Aug 18 '17 at 06:41
  • 1
    I've converted our code to a runnable snippet and it "works" (though it is unreliable). Please explain how you determined that it doesn't work. Note that depending on the host timezone, the date and time in the OP may fall on a different date (e.g. where the offset is -0700). – RobG Aug 18 '17 at 06:46
  • Hi Rob, when i use format() it works. Remove format and console the result. – Maclean Pinto Aug 18 '17 at 08:26

1 Answers1

4

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>
Shubham Khatri
  • 270,417
  • 55
  • 406
  • 400