This discussion is archived
6 Replies Latest reply: Jul 31, 2013 2:08 PM by Nicolette RSS

Convert string to date in javascript

skapex Newbie
Currently Being Moderated

Hi,

On my apex page I have a hidden item P1_STORE_DT which stores date in '01-AUG-13' format.

Now what i need to do is convert this into date in javascript

On page load i execute below code

 

var d=new Date($v('P1_STORE_DT'));

alert(d);

--further code

 

new Date works fine in chrome.

But in IE it shows NaN.

 

How to convert my string into specific format so that it works fine in IE and chrome.

  • 1. Re: Convert string to date in javascript
    Recx Ltd Explorer
    Currently Being Moderated

    It doesn't sound right that you should be passing dates in this format, can you not set P1_STORE_DT to be a better format (at least 4 digit year)? If you are still determined to go with this the following function will help until 2099 then will break seriously.

     

    -- warning code will break past 2099.

    var d = (function getMyDate(dt){i=['JAN','FEB','MAR','APR','MAY','JUN','JUL','AUG','SEP','NOV','DEC'];s=dt.split('-');return new Date('20' + s[2], i.indexOf(s[1]), s[0])})($v('P1_STORE_DT'));

    alert(d)

    --further code

     

    Pass the date with a 4 digit year and remove the '20' + from the code

     

    regards

  • 2. Re: Convert string to date in javascript
    Nicolette Expert
    Currently Being Moderated

    RecxLtd

     

    It is also important to note that your code only works with the formatmask 'DD-MON-YY'.

    In chrome how ever the formatmask 'DD-MON-RRRR' is used.

    Meaning that 13-AUG-50 will return the date Sun Aug 13 1950 and not Sat Aug 13 2050 in Chrome.

     

    Since the OP hasn't supplied the formatmask he is using and  'DD-MON-RRRR' is a common formatmask I have taken the liberty to supply a modified version of your code that will work with the format mask 'DD-MON-RRRR'.

     

    function getMyDate(dt){
      var i,s,today,currentYear, currentCentury, year;
      /*Array of the month names*/
      i=['JAN','FEB','MAR','APR','MAY','JUN','JUL','AUG','SEP','OCT','NOV','DEC'];
      /*Split the input value in its different parts*/
      s=dt.split('-');
      if (s[2].length>2){
        /*More then two characters then the century is included*/
        return new Date(s[2], i.indexOf(s[1]), s[0]);
      } else {
        /*Determine the current century*/
        today = new Date();
        currentYear = today.getFullYear().toString();
        currentCentury = new Number(currentYear.substr(0,2));    
        /*Make the year a number*/
        year = new Number(s[2]);
        if (year < 49){
          /*Before 49 then this century*/
          return new Date(currentCentury.toString()+ year.toString(), i.indexOf(s[1]), s[0]);
        } else {
          /*After 49 then the last century*/
          return new Date((currentCentury-1)+year.toString(), i.indexOf(s[1]), s[0]);
        } 
      } 
    }

     

    OP

    Note that the supplied code only works for the formatmasks 'DD-MON-RRRR' and 'DD-MON-RR'  and if the language is english.

     

    Nicolette

    PS RecxLtd The month October is also missing from your month array.

  • 3. Re: Convert string to date in javascript
    Recx Ltd Explorer
    Currently Being Moderated

    Good work Nicolette, I never liked October anyway However the OP did specify the mask.. "stores date in '01-AUG-13' format"

     

    Basing dates on a 2 digit year is always recipe for disaster, for example I just put my grandmothers birthday in 13-MAR-38 your code claims she isn't even born yet. Even though DD-MON-YYYY is a common format, it is not well supported (see link below).

     

     

    If you have control over the date mask, setting it to be "YYYY/MM/DD" appears to be the best when it comes to Javascript.

     

     

    There is a great breakdown here http://dygraphs.com/date-formats.html

     

    regards

  • 4. Re: Convert string to date in javascript
    Nicolette Expert
    Currently Being Moderated

    RecxLtd

     

    RecxLtd wrote:

    However the OP did specify the mask.. "stores date in '01-AUG-13' format"

    And with stating the input like that the OP left open what the actual formatmask being used is.

     

    Typing in 01-AUG-13 with a formatmask of DD-MON-RRRR gives you a correct date.

    And if you don't mean 2013 but 1913 just type in 01-AUG-1913 . And the formatmask still works to give a correct date.

    Even the formatmask DD-MON-RR is possible with the given input. And giving 1950 for the year 50.

     

    Secondly the OP stated that the Chrome was giving the correct output.

    And some testing shows that Chrome is actually using the DD-MON-RRRR format for converting the string to a date.

     

    Nicolette

  • 5. Re: Convert string to date in javascript
    Recx Ltd Explorer
    Currently Being Moderated

    Sure, I completely agree. But to convert that by using the JavaScript Date constructor it would be wise to use YYYY/MM/DD then in that case we wouldn't be looking for workarounds and could just do;

     

    mydate = new Date($v('P1_STORE_DT'));

     

    as per the OPs original request, and would work in IE instead of returning NaN. I understand DD-MON-RRRR with a two digit year is a hangover from Y2K and your code is completely correct in emulating that.

     

    I wonder how difficult it would be  to get a; to_char(SYSDATE, 'YYYY/MM/DD') in the query? Then all this would go away.

     

    regards

  • 6. Re: Convert string to date in javascript
    Nicolette Expert
    Currently Being Moderated

    RecxLtd

     

    Let's just go that we both assume a differend formatmask when presented with the date 01-AUG-13.

    It is that the OP stated that chrome gave the expected result and a little test in chrome showed Thu Aug 01 2013 for 01-AUG-13.

    Other wise he/she could very well mean 2001-AUG-13 or 01-AUG-0013. Meaning we were both wrong.

     

    As for RRRR being a hang over from Y2K I don't know but I think of RRRR as an input formatmask while YYYY is more for presentation.

     

    Just though to provide the adapted code for anyone else stumbling on this tread looking for a javascript date to string conversion maybe even in the context of a client side validation.

     

    As for my code being completely correct. Unfortunately that isn't the case.

    if (year < 49){  
    /*Before 49 then this century*/  
    return new Date(currentCentury.toString()+ year.toString(), i.indexOf(s[1]), s[0]);  
        } else {  
    /*After 49 then the last century*/  
    return new Date((currentCentury-1)+year.toString(), i.indexOf(s[1]), s[0]);  
        }  

    Should be

    if (year < 50){  
          /*Before 50 then this century*/  
          return new Date(currentCentury.toString()+ year.toString(), i.indexOf(s[1]), s[0]);  
    } else {  
     /*After 49 then the last century*/  
          return new Date((currentCentury-1)+year.toString(), i.indexOf(s[1]), s[0]);   }   

     

    Nicolette

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points