-4

I have a date string like this 20/09/2018 12:00 AM. I need to stop to put the previous date than today. I have searched the web for it, but no answer found with this format.

I need the default date format of JavaScript so that I can compare with new Date() value. When I use the following format it show the message that says invalid date because of my dd/MM/yyyy hh:mm tt format.

alert(new Date("20/09/2018 12:00 AM"));
halfer
  • 19,824
  • 17
  • 99
  • 186
Sumon Bappi
  • 1,937
  • 8
  • 38
  • 82
  • 3
    Why "using jquery"? This has nothing to do with dom manipulation. This is purely a logical problem. – Taplar Sep 19 '18 at 20:55
  • i need to use this in script. to check the date is not from previous. I am having the value from input field and need to cust to javascript date object. – Sumon Bappi Sep 19 '18 at 20:57
  • Possible duplicate of [Javascript Date Parse with specific locale](https://stackoverflow.com/questions/50781887/javascript-date-parse-with-specific-locale) – Stephen P Sep 19 '18 at 20:58
  • 1
    javascript != jquery. One is a language, the other a framework. There is no need to use jquery here. – Igor Sep 19 '18 at 20:58
  • 1
    I recommend you try to solve this using [*moment.js*](https://momentjs.com/), you can specify the format when parsing a date/time string and there are plenty of convenience functions like `isAfter` and `isBefore` – Igor Sep 19 '18 at 21:00
  • 1
    I was about to mention _moment.js_ also. You can do this: `var m = moment("20/09/2018 3:14 PM", "DD/MM/YYYY h:mm a"); m.toDate();` – Stephen P Sep 19 '18 at 21:11
  • @StephenP can you add this as answer so that I can accept it – Sumon Bappi Sep 19 '18 at 21:36
  • Possible duplicate of [Converting a string to a date in JavaScript](https://stackoverflow.com/questions/5619202/converting-a-string-to-a-date-in-javascript) – Twisty Sep 19 '18 at 21:40

2 Answers2

3

Igor recommended using moment.js to solve this — it is a widely used date/time library.

With moment.js you can do this:

var m = moment("20/09/2018 3:14 PM", "DD/MM/YYYY h:mm a");
var d = m.toDate();

The first line creates a "moment" object by parsing the date according to the format string specified as the second argument. See http://momentjs.com/docs/#/parsing/

The second line gets the native javascript Date object that the moment object encapsulates; however, moment can do so many things you may not need to get back that native object.

See the moment docs.

Stephen P
  • 14,422
  • 2
  • 43
  • 67
0

Your format isn't valid, thus you're getting invalid date error. So, using your format(dd/MM/yyyy hh:mm tt) we'll grab the year, month, day, hours and the minutes, then we'll reformat it as an acceptable format by the Date constructor and create a Date instance.

Here's a function that do all what being said and returns a Date instance which you can compare it with another Date instance:

function convertToDate(str) {
  // replace '/' with '-'
  str = str.replace(/\//ig, '-');
  
  /**
  * extracting the year, month, day, hours and minutes.
  * the month, day and hours can be 1 or 2 digits(the leading zero is optional).
  * i.e: '4/3/2022 2:18 AM' is the same as '04/03/2022 02:18 AM' => Notice the absence of the leading zero.
  **/
  var y = /\-([\d]{4})/.exec(str)[1],
      m = /\-([\d]{2}|[\d])/.exec(str)[1],
      d = /([\d]{2}|[\d])\-/.exec(str)[1],
      H = /\s([\d]{2}|[\d]):/.exec(str)[1],
      i = /:([\d]{2})/.exec(str)[1],
      AMorPM = /(AM|PM)/.exec(str)[1];
  
  // return a Date instance.
  return new Date(y + '-' + m + '-' + d + ' ' + H + ':' + i + ' ' + AMorPM)
}

// testing...
var str1 = '20/09/2018 12:00 AM';
var str2 = '8/2/2018 9:00 PM'; // leading zero is omitted.
console.log(convertToDate(str1));
console.log(convertToDate(str2));

The Date depends on the user's/server's location, two users may have different results.

Learn more about Date.

Hope I pushed you further.

ThS
  • 4,597
  • 2
  • 15
  • 27