0

I have an input form for date, which gives me a string such as 06/01/2015

Using it I need to get two variables in time format for the beginning of the day and the end of the day (00:00:00, and 23:59:59) in time (milliseconds) format such as 1433206800000, 1433205690000. Can someone give me a tip on how to do it in Javascript.

Thank you for your time.

3
  • Please share what you have tried so far. Commented Jun 1, 2015 at 12:57
  • Try momentjs.com Commented Jun 1, 2015 at 12:58
  • 1
    Or try vanilla JS which is quite capable of doing the job without any plugins Commented Jun 1, 2015 at 13:07

2 Answers 2

4
var d = new Date('your_date_string');

var start = Number(d.setHours(0,0,0,0));
var end = Number(d.setHours(23,59,59,999));
//OR
var start = d.setHours(0,0,0,0).getTime();
var end = d.setHours(23,59,59,999).getTime();

start and end are your variables

See setHours docs here

Sign up to request clarification or add additional context in comments.

3 Comments

or d.setHours(0,0,0,0).getTime()
If you use Number(date) that give the save as of date.getTime()
Yes, but it is using the official interface instead of having JS cast the value
1

The Date class is smart enough to know how to convert standard date string formats into a date, so you can just create new date objects, and then use the getTime() function to get it in milliseconds.

var dateStr = "06/01/2015",
    dayStartStr = " 00:00:00.000",
    dayEndStr = " 23:59:59.999";

var startDate = new Date(dateStr + dayStartStr);
var endDate = new Date(dateStr + dayEndStr);

console.log("start = " + startDate.getTime());
console.log("end = " + endDate.getTime());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.