2

I want to parse dates (string date format to javascript date format), but I have various string date format like YYYY-MM-DD, DD-MM-YYYY and 'DD/MM/YYYY' and so..

Is there any generic procedure or way to convent from these string date format to javascript date format?

  • This question is numerous times better stated than the one pointed at!—Actually, the one pointed at is totally out of shape and should be closed. – Robert Siemer Feb 11 '20 at 13:57
  • This question is numerous times better stated than the one pointed at!—Actually, the one pointed at is closed for being unclear what is asked! – Robert Siemer Feb 12 '20 at 17:10

4 Answers4

2

These are two libraries that I use for that:

Robert Siemer
  • 32,405
  • 11
  • 84
  • 94
mram888
  • 4,899
  • 5
  • 33
  • 59
  • 1
    Just two? There is also [XDate](http://arshaw.com/xdate/), [fecha.js](https://github.com/taylorhakes/fecha), [date-fns](https://www.google.com.au/url?sa=t&rct=j&q=&esrc=s&source=web&cd=4&ved=0ahUKEwjyo-L9r43TAhVCxFQKHfQXDNQQFgg0MAM&url=https%3A%2F%2Fdate-fns.org%2F&usg=AFQjCNEYak1EBCviOaBClNIng5-WItUaDQ&bvm=bv.151426398,d.cGw&cad=rja), [Later.js](http://bunkat.github.io/later/), [Countdown.js](http://countdownjs.org), [Cubism.js](http://square.github.io/cubism/) (D3 plugin), [js-joda](https://www.npmjs.com/package/js-joda), … ;-) – RobG Apr 05 '17 at 13:06
1

Here userFormat string could be in these format 'DD-MM-YYYY', 'YYYY-MM-DD', 'DD/MM/YYYY' etc..

function parseDate(dateString, userFormat) {
    var delimiter, theFormat, theDate, month, date, year;
    // Set default format if userFormat is not provided
    userFormat = userFormat || 'yyyy-mm-dd';

    // Find custom delimiter by excluding
    // month, day and year characters
    delimiter = /[^dmy]/.exec(userFormat)[0];

    // Create an array with month, day and year
    // so we know the format order by index
    theFormat = userFormat.split(delimiter);

    //Create an array of dateString.
    theDate = dateString.split(delimiter);
    for (var i = 0, len = theDate.length; i < len; i++){
      //assigning values for date, month and year based on theFormat array.
      if (/d/.test(theFormat[i])){
        date = theDate[i];
      }
      else if (/m/.test(theFormat[i])){
        month = parseInt(theDate[i], 10) - 1;
      }
      else if (/y/.test(theFormat[i])){
        year = theDate[i];
      }
    }
    return (new Date(year, month, date));
}
Niks Jain
  • 1,617
  • 5
  • 27
  • 53
  • wow..its very very generic procedure..thats what i wanted actually great thanks. –  Nov 05 '12 at 10:54
0

Just go get datejs.

http://www.datejs.com/

...and never bother with writing your own Javascript date functions again.

Paul Alan Taylor
  • 10,474
  • 1
  • 26
  • 42
-1
function dateParsing(toDate, dateFormat) {
    var dt = Date.parseInvariant(todate, dateFormat); // pass date and your desired format e.g yyyy/M/dd            
    alert(dt); // to check date
}
Emil
  • 7,220
  • 17
  • 76
  • 135
  • `Date.parseInvariant()` is not a Javascript function. – Roland Jul 27 '17 at 14:03
  • Actually, Date.parseInvariant in an JScript (Microsoft-only) "Date Type Extensions". "_Date extensions are part of the Microsoft Ajax Library and add functionality to the JavaScript Date object._" https://msdn.microsoft.com/en-us/ie/bb310850(v=vs.94) – Peter Jun 09 '19 at 10:16