1

I have a JSON object with these two fields...

"start_datetime": "3/9/2016 8:00",
"stop_datetime": "3/18/2016 16:00",

How can I tell if the current date/time is between these two?

16:00 - 4PM

08:00 - 8AM

3
  • new Date("3/18/2016 16:00") Commented Mar 10, 2016 at 2:06
  • But how would I be able to tell if I am in between these two dates? Commented Mar 10, 2016 at 2:07
  • Date.now() > new Date("3/9/2016 8:00") && Date.now() < new Date("3/18/2016 16:00"), have fun @softwareisfun Commented Mar 10, 2016 at 2:11

1 Answer 1

2

DateTime objects can be compared with the standard comparison operators to tell if one is before the other.

var start = new Date(obj.start_datetime);
var end = new Date(obj.end_datetime);
var now = new Date();

if (now > start && now < end) {
    // current date and time between start and end
} else {
   // current date and time not between start and end
}
Sign up to request clarification or add additional context in comments.

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.