Tuesday, October 20, 2015

Convert String to Date in DD/MM/YYYY from Java scripts

If you try to convert DD/MM/YYYY String to date it will give you Invalid Date Error

  new Date("31/05/2015")
 Invalid Date

Solution is to convert it to correct format using REGEX

Some samples are shown below

new Date("31/05/2015".replace(/(\d{2})\/(\d{2})\/(\d{4})/, "$2/$1/$3"))
Sun May 31 2015 00:00:00 GMT+1000 (AUS Eastern Standard Time)

new Date("31/05/2015 03:24 PM".replace(/(\d{2})\/(\d{2})\/(\d{4})/, "$2/$1/$3"))
Sun May 31 2015 15:24:00 GMT+1000 (AUS Eastern Standard Time)

No comments:

Post a Comment