0

I have an ajax request that returns a date and time. my ajax output is 7/22/2017, 3:35:51 PM, How can I get the time and have a condition between 9:00am to 5:00pm. Thank you in advance.

$.ajax({
          async: false,
          type: "POST",
          url: apicall,
          success: function (response) {
           console.log(response);
               var offsets = response.dstOffset * 1000 + response.rawOffset * 1000 // get DST and time zone offsets in milliseconds
            var localdate = new Date(timestamp * 1000 + offsets) // Date object containing current time of Tokyo (timestamp + dstOffset + rawOffset
              console.log(localdate.toLocaleString()) // Display current Tokyo date and time

          }

    }); 
John Wick
  • 511
  • 1
  • 5
  • 20

2 Answers2

0

Using momentjs would be easier, in my opinion

getting the current time is as simple as moment()

to check if it's between a time range: [http://momentjs.com/docs/#/query/is-between/]

justelouise
  • 714
  • 1
  • 5
  • 20
0

This works for me.

You can create 3 new dates, with your string date, one to check, second to be the upper date to check and the third to be the lower date to check. The second and the third you need to update to 9:00 AM and 5:00 AM respectively. Then you can check your input date.

// yes:
var s = '7/22/2017, 5:35:51 AM';
var d = new Date(s);
var upperD = new Date(s);
var lowerD = new Date(s);
upperD.setHours(9);
upperD.setMinutes(0);
upperD.setSeconds(0);
lowerD.setHours(5);
lowerD.setMinutes(0);
lowerD.setSeconds(0);
console.log(upperD >= d && d >= lowerD ? 'yes' :'no');

// no:
var s = '7/22/2017, 4:35:51 AM';
var d = new Date(s);
var upperD = new Date(s);
var lowerD = new Date(s);
upperD.setHours(9);
upperD.setMinutes(0);
upperD.setSeconds(0);
lowerD.setHours(5);
lowerD.setMinutes(0);
lowerD.setSeconds(0);
console.log(upperD >= d && d >= lowerD ? 'yes' :'no');
sidanmor
  • 5,079
  • 3
  • 23
  • 31