12

I have a page with a number of variables that contain a date in string format (yyyy-mm-dd) which originates from using moment.js.

Is there a way I can pass such a variable to a Javascript date object resp. to convert it to a Javascript date object ? I am not interested in the time so as long as I can get the date converted to a date object that would be great.

I tried the following but this doesn't work and I couldn't find a way using moment.js:

var newVar = new Date(dateVar);

Many thanks for any help with this, Tim.

user2571510
  • 11,167
  • 39
  • 92
  • 138
  • 2
    you can call to toDate() method of momentjs instead of getting it as a string... – Arun P Johny Mar 05 '14 at 04:00
  • 2
    Refer to this other question: http://stackoverflow.com/questions/8099681/is-there-a-php-date-equivalent-in-javascript-jquery – Femi Mar 05 '14 at 04:00
  • 2
    `var newVar = new Date(dateVar);` seems to be working for me. Try `new Date("2014-03-05")` in the console – tewathia Mar 05 '14 at 04:07

3 Answers3

28

first of all i will say following should work for you..

var dateVar = "2010-10-30";
var d=new Date(dateVar);

if you say above not working check the below one -

var dateVar = "2010-10-30";
var dsplit = dateVar.split("-");
var d=new Date(dsplit[0],dsplit[1]-1,dsplit[2]);

for the proof check the jsfiddle.. both is working fine.. JSFiddle

Deepak Sharma
  • 4,124
  • 1
  • 14
  • 31
  • 1
    Thanks a lot. The second one works fine for me. Would that be faster than using Regex in such a case ? – user2571510 Mar 05 '14 at 04:11
  • 2
    Regex for what?? to split the date string?? i will suggest you to use the same code.. as we just need to split the string with **Hyphen (-)** which is a simple task. so i will not suggest you to create one more object just to split .. – Deepak Sharma Mar 05 '14 at 04:13
  • 1
    You are right. Thanks for the explanation, this works great - just wanted to check. Thanks again ! – user2571510 Mar 05 '14 at 04:15
5

console.log() has been use to show the output , run this and you will understand the code

    <script type='text/javascript'>
    var StringDate = "2013-4-13"
    var date = StringDate.split("-"); 
    console.log(date[0]);
    console.log(date[1]);
    console.log(date[2]);

    NewDate = new Date(date[0],date[1]-1,date[2]);//Date object
    console.log(NewDate);
    </script>
sanjeev
  • 4,405
  • 2
  • 19
  • 27
  • 1
    Thanks - this looks good ! As I have to do this with several strings in a row I would like to have less code for each of them. Is there a way to achieve the same with Regex as well ? – user2571510 Mar 05 '14 at 04:08
1

To solve this problem I a made function who manage the change from text to date:

My examples works with date as this: Jun/1/2016 to 2016-06-01 you can rebuild the function to make your format works...

The cero's at the left of the numbers are added to match the date type format.

function textoafecha(texto)
        {

            hasNumber = /\d/;

            // Contiene el pedazo del texto que contiene el mes
            mestexto = texto.substr(0,3);
            // Contiene el pedazo de texto que contiene el primer numero del dia
            diatextonumero1 = texto.substr(4,1);
            diatextonumero2 = texto.substr(5,1);

            // Si el texo contiene un numero...
            if (hasNumber.test(diatextonumero2))
               {
                 dia = texto.substr(4,2);
                 anotexto = texto.substr(7,4);
               }
            else
               {
                 dia = texto.substr(4,1);
                 dia = "0"+ dia;
                 anotexto = texto.substr(6,4); 
               }


            switch (mestexto) 
            {
              case "Jan" : mesnumero = "01";
                           break;   

              case "Feb" : mesnumero = "02";
                           break;   

              case "Mar" : mesnumero = "03";
                           break;   

              case "Apr" : mesnumero = "04";
                           break;   

              case "May" : mesnumero = "05";
                           break;   

              case "Jun" : mesnumero = "06";
                           break;   

              case "Jul" : mesnumero = "07";
                           break;   

              case "Aug" : mesnumero = "08";
                           break;   

              case "Sep" : mesnumero = "09";
                           break;   

              case "Oct" : mesnumero = "10";
                           break;   

              case "Nov" : mesnumero = "11";
                           break;   

              case "Dec" : mesnumero = "12";
                           break;   

              default : break;

            }

            fechaformateada = anotexto + "-" + mesnumero + "-" + dia;           
            return fechaformateada;

        }       
Ricardo Rivera Nieves
  • 1,305
  • 2
  • 8
  • 7