1

I am working on Javascript dates, I have one date which is in the form of 20 Jun 13, I need to convert it into a Date Object, I used Date.parse() which returns 1 June 2013.

var frmDt = '20 Jun 13';
alert(Date.parse(frmDt));    

Which is the solution?

Erik Schierboom
  • 16,301
  • 10
  • 64
  • 81
user2484678
  • 31
  • 1
  • 5
  • I think this may help: http://stackoverflow.com/questions/17267230/how-to-convert-the-following-string-to-the-correct-string/17267449#17267449 – HMR Jun 27 '13 at 07:07
  • Not directly but it may help: http://www.datejs.com/ – Sven Schneider Jun 27 '13 at 07:07
  • 2
    You need to show the exact code you use, along with example input. – JJJ Jun 27 '13 at 07:08
  • @Juhana Are you kidding me? Even before the question was edited it was quite clear the input is `20 Jun 13` and the code used is `Date.parse('20 Jun 13')` Even though the data.parse would result in NaN instead of the outcome the OP claimed. – HMR Jun 27 '13 at 07:28
  • 1
    @HMR You'd be surprised how many people say their input is "X" when it's actually "Y". People tend to oversimplify their questions. A mismatch in output is a strong hint that's happening (although in this case it's probably just that JS engines have different ways to handle invalid date strings). – JJJ Jun 27 '13 at 08:15
  • @Juhana Yes, that makes sense in Chrome you'll get the right output. new Date(Date.parse('20 jun 13')).toDateString()//="Thu Jun 20 2013" – HMR Jun 27 '13 at 09:12

3 Answers3

1

I found date handling in javascript made extremely easier by using momentJs, which allows you to construct moment objects that actually wrap native javascript Date objects in a very liberal way, passing in strings of many different formats and actually being able to get a date object, in a way that is way more reliable than Date.parse().

You can also pass in a date format string, so your case would be something like moment('20 Jun 13', 'DD MMM YY').

Raibaz
  • 9,280
  • 10
  • 44
  • 65
0

I have taken the code posted in my comment and turned it into a function, made the jan,feb,mar case insensitie and the seperator character between day month year can be any character (as long is there is one). so any format dd mmm yy or dd/mmm/yy or d mmm yy will work:

function toDate(str){
    var months={
     "JAN":1,
     "FEB":2,
     //... other months
     "JUN":6,
     //... other months
     "DEC":12
    }
    var r=/^(\d{1,2}).(\w{3}).(\d{2}$)/;
    if(r.test(str)===false){
      throw new Error("Invalid date string:"+str);
    }
    var replaceFunction=function(){
     var years=parseInt(arguments[3],10);
     var m=months[arguments[2].toUpperCase()];
     if(typeof m==="undefined"){
       throw new Error("Invalid month name:"+arguments[2]);
     }
     var days=arguments[1]
     m=(m<9)?"0"+m:m;
     days=(days.length===1)?days="0"+days:days;
     years=(years>50)?years="19"+years:"20"+years;
     return m+"/"+days+"/"+years;
    };
    return new Date(str.replace(r,replaceFunction));
}
console.log(toDate("20 Jun 13"));
HMR
  • 37,593
  • 24
  • 91
  • 160
0

d3.js has very robust capabilities for parsing and formatting dates. See https://github.com/mbostock/d3/wiki/Time-Formatting.

  • Are you sure including a chart drawing library is the best way to parse dates when there are other libraries that specifically handle just date parsing and formatting? – Raibaz Jun 27 '13 at 08:38
  • No, I'm not sure, but hey, we live in a world where people include 100K libraries to get a little syntactic sugar for DOM manipulation. –  Jun 27 '13 at 21:00