rexroxm
rexroxm

Reputation: 878

Check weather the entered datetime is more than 72 hours from now?

I have a DateTime picker that generates the date and time for a textbox in the following format,

01 May, 2017 - 03:45 pm

Now I need to check whether this DateTime is more than 72 hours from now. For that, I use the following algorithm in Javascript.

function chk() {
  try {
    debugger;
    var a = document.getElementById("txtdate").value;

    var adatepart = a.substring(0, 12).replace(",", "");

    //convert date part of txtbox
    var b = parseDate(adatepart);

    //getting number of hours of textbox from 1 jan 1970 midnight
    var seconds = new Date().getTime() / (3600 * 1000);

    //getting number of hours from 1 jan 1970
    var seconds1 = b.getTime() / (3600 * 1000);

    //adding additional hours and 12 hours if it's past noon
    seconds1 += parseInt(a.substring(15, 17)) + (a.substring(21, 23) == "pm" ? 12 : 0);
    var diff = seconds1 - seconds;
    if (diff > 72 || diff < 0) {
      alert("The selected date and time cannot be more than 72 hours from now");
    }
  } catch (err) {
    alert(err.message);
  }
}

function parseDate(input) {
  var map = { Jan: 0, Feb: 1, Mar: 2, Apr: 3, May: 4, Jun: 5, Jul: 6, Aug: 7, Sep: 8, Oct: 9, Nov: 10, Dec: 11 };
  input = input.split(" ");
  return new Date(input[2], input[1] in map ? map[input[1]] : input[1] - 1, input[0]);
}

This algorithm works fine. But I believe there is a shorter way where I can directly compare the date.now and the datetime in textbox. Perhaps through jQuery. Is there any shorter way?

Upvotes: 2

Views: 1150

Answers (3)

Rahul
Rahul

Reputation: 42

This Might help

diff = (parseDate(e.substring(0, 12).replace(",", "")).getTime() / (3600 * 1000) + parseInt(e.substring(15, 17)) + (e.substring(21, 23) == "pm" ? 12 : 0)) - new Date().getTime() / (3600 * 1000);

if (diff < -72) {
//Your code 
}

Upvotes: 1

RobG
RobG

Reputation: 147413

Starting with the string "01 May, 2017 - 03:45 pm", you can generate a Date, subtract 72 hours, then see if it's greater than now.

There are a couple of good libraries for parsing and formatting dates (e.g. moment.js and fecha.js are fine), or you can use a custom function.

// Parse date in format 01 May, 2017 - 03:45 pm
function parseDate(s) {
  var months = ['jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov','dec'];
  var b = s.match(/\w+/g);
  var invalidDate = new Date(NaN);
  if (!b || b.length != 6) return invalidDate;

  var d = new Date(b[2],
                   months.indexOf(b[1].toLowerCase()),
                   b[0],
                   b[3]%12 + (/pm/.test(b[5])? 12 : 0),
                   b[4],);
   return isNaN(d)? invalidDate : d;  
}

// Examples
var s = '01 May, 2017 - 03:45 pm';
var d = parseDate(s);
d.setUTCHours(d.getUTCHours() - 72);

console.log('Is ' + s + ' less than 72 hours from now? ' + (d < Date.now));

var s = '22 Apr, 2017 - 03:45 pm';
var d = parseDate(s);
d.setUTCHours(d.getUTCHours() - 72);

console.log('Is ' + s + ' less than 72 hours from now? ' + (d < Date.now()));

Note that adding 72 hours using UTC methods means 72 elapsed hours, even over daylight saving boundaries.

Upvotes: 0

Gaurav Gandhi
Gaurav Gandhi

Reputation: 3201

Use moment.js

Here is how you can implement it in your code,

function chk() {
  var txtDate = moment(document.getElementById("txtdate").value, "DD MMM, YYYY - hh:mm a");
  var diffInHours = txtDate.diff(moment(), 'hours');

  if (diff > 72 || diff < 0) {
    alert("The selected date and time cannot be more than 72 hours from now");
  }
};

Now diffInHours will have the difference of time in hours between now and selected DateTime.

You don't need to parse it too.

Upvotes: 2

Related Questions