3

How can i check the date picker date which are selected is less than current date.

My date picker formate is [MON d,YYYY] Like [Feb 6, 2012],

if i pick up old date in textbox how can i check this date is less than current date.

gdoron
  • 147,333
  • 58
  • 291
  • 367
vims
  • 29
  • 1
  • 1
  • 2

2 Answers2

3

Use $.datepicker.parseDate(format, string) to turn your date in to JavaScript Date, get current date by var now = new Date();

After that, You can just use < operator to check whether the date picked is less than the current date.

EDIT: Assuming you are using jquery ui date picker

Johnbabu Koppolu
  • 3,212
  • 2
  • 22
  • 34
2

This seems to do the trick...

var isFutureDate = new Date < new Date(
                               str
                               .match(/^\[(\w{3})\s(\d+),\s(\d{4})\]$/)
                               .slice(1)
                               .join(' ')
                              );

jsFiddle.

This creates a new Date object which defaults to the current client time, and then constructs a second Date object passing a modified version of your user input which can be parsed by the Date constructor. The < operators calls the valueOf() of both dates, which returns the milliseconds since the Unix epoch (equivalent of calling getTime()).

You could take into account leading and trailing whitespace on the input by using $.trim() or wrapping the regex with \s*.

You could swap new Date with $.now(), which is slightly fewer characters

alex
  • 479,566
  • 201
  • 878
  • 984