Javascript new Date Cross Browser Format Problem





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















I'm having problems with date string formatting using Javascript. On IE the date format is MM-DD-YYYY but on Firefox it's YYYY-MM-DD. Chrome works both of them but when I choose one of the formats, the other browser gives me an Invalid Date warning.



My code as an example (works on IE but not on Firefox):



  var dateSTR = "2015-09-29";
var date = new Date(dateSTR.replace(/(d{4})-(d{2})-(d{2})/,"$2-$3-$1"));
date.setUTCHours(date.getHours());
date.setUTCMinutes(date.getMinutes());
return date.toLocaleDateString();


https://jsfiddle.net/kmmna8c0/










share|improve this question

























  • I want to find a solution without a plugin.

    – Kamuran Sönecek
    Sep 29 '15 at 7:13






  • 1





    "On IE date format is MM-DD-YYYY" is a very broad statement. In what way is that "the date format"? The parameter to new Date(dateString) is documented to be in ISO-8601 or RFC2822 format, so I'd expect you to just be able to use dateSTR directly, without any messing around.

    – Jon Skeet
    Sep 29 '15 at 7:14






  • 1





    possible duplicate of Javascript Date() constructor doesn't work

    – CBroe
    Sep 29 '15 at 7:16











  • “I want to find a solution without a plugin” – well then write code that parses your input date format into its single parts yourself, and then feed them to the Date constructor version that takes single arguments for each one of them.

    – CBroe
    Sep 29 '15 at 7:18











  • @JonSkeet when I use dateSTR directly, it does not work on IE. CBroe thanks for your link

    – Kamuran Sönecek
    Sep 29 '15 at 7:22




















1















I'm having problems with date string formatting using Javascript. On IE the date format is MM-DD-YYYY but on Firefox it's YYYY-MM-DD. Chrome works both of them but when I choose one of the formats, the other browser gives me an Invalid Date warning.



My code as an example (works on IE but not on Firefox):



  var dateSTR = "2015-09-29";
var date = new Date(dateSTR.replace(/(d{4})-(d{2})-(d{2})/,"$2-$3-$1"));
date.setUTCHours(date.getHours());
date.setUTCMinutes(date.getMinutes());
return date.toLocaleDateString();


https://jsfiddle.net/kmmna8c0/










share|improve this question

























  • I want to find a solution without a plugin.

    – Kamuran Sönecek
    Sep 29 '15 at 7:13






  • 1





    "On IE date format is MM-DD-YYYY" is a very broad statement. In what way is that "the date format"? The parameter to new Date(dateString) is documented to be in ISO-8601 or RFC2822 format, so I'd expect you to just be able to use dateSTR directly, without any messing around.

    – Jon Skeet
    Sep 29 '15 at 7:14






  • 1





    possible duplicate of Javascript Date() constructor doesn't work

    – CBroe
    Sep 29 '15 at 7:16











  • “I want to find a solution without a plugin” – well then write code that parses your input date format into its single parts yourself, and then feed them to the Date constructor version that takes single arguments for each one of them.

    – CBroe
    Sep 29 '15 at 7:18











  • @JonSkeet when I use dateSTR directly, it does not work on IE. CBroe thanks for your link

    – Kamuran Sönecek
    Sep 29 '15 at 7:22
















1












1








1








I'm having problems with date string formatting using Javascript. On IE the date format is MM-DD-YYYY but on Firefox it's YYYY-MM-DD. Chrome works both of them but when I choose one of the formats, the other browser gives me an Invalid Date warning.



My code as an example (works on IE but not on Firefox):



  var dateSTR = "2015-09-29";
var date = new Date(dateSTR.replace(/(d{4})-(d{2})-(d{2})/,"$2-$3-$1"));
date.setUTCHours(date.getHours());
date.setUTCMinutes(date.getMinutes());
return date.toLocaleDateString();


https://jsfiddle.net/kmmna8c0/










share|improve this question
















I'm having problems with date string formatting using Javascript. On IE the date format is MM-DD-YYYY but on Firefox it's YYYY-MM-DD. Chrome works both of them but when I choose one of the formats, the other browser gives me an Invalid Date warning.



My code as an example (works on IE but not on Firefox):



  var dateSTR = "2015-09-29";
var date = new Date(dateSTR.replace(/(d{4})-(d{2})-(d{2})/,"$2-$3-$1"));
date.setUTCHours(date.getHours());
date.setUTCMinutes(date.getMinutes());
return date.toLocaleDateString();


https://jsfiddle.net/kmmna8c0/







javascript date






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 16:06









Jonathan

15.3k34111184




15.3k34111184










asked Sep 29 '15 at 7:10









Kamuran SönecekKamuran Sönecek

1,93021743




1,93021743













  • I want to find a solution without a plugin.

    – Kamuran Sönecek
    Sep 29 '15 at 7:13






  • 1





    "On IE date format is MM-DD-YYYY" is a very broad statement. In what way is that "the date format"? The parameter to new Date(dateString) is documented to be in ISO-8601 or RFC2822 format, so I'd expect you to just be able to use dateSTR directly, without any messing around.

    – Jon Skeet
    Sep 29 '15 at 7:14






  • 1





    possible duplicate of Javascript Date() constructor doesn't work

    – CBroe
    Sep 29 '15 at 7:16











  • “I want to find a solution without a plugin” – well then write code that parses your input date format into its single parts yourself, and then feed them to the Date constructor version that takes single arguments for each one of them.

    – CBroe
    Sep 29 '15 at 7:18











  • @JonSkeet when I use dateSTR directly, it does not work on IE. CBroe thanks for your link

    – Kamuran Sönecek
    Sep 29 '15 at 7:22





















  • I want to find a solution without a plugin.

    – Kamuran Sönecek
    Sep 29 '15 at 7:13






  • 1





    "On IE date format is MM-DD-YYYY" is a very broad statement. In what way is that "the date format"? The parameter to new Date(dateString) is documented to be in ISO-8601 or RFC2822 format, so I'd expect you to just be able to use dateSTR directly, without any messing around.

    – Jon Skeet
    Sep 29 '15 at 7:14






  • 1





    possible duplicate of Javascript Date() constructor doesn't work

    – CBroe
    Sep 29 '15 at 7:16











  • “I want to find a solution without a plugin” – well then write code that parses your input date format into its single parts yourself, and then feed them to the Date constructor version that takes single arguments for each one of them.

    – CBroe
    Sep 29 '15 at 7:18











  • @JonSkeet when I use dateSTR directly, it does not work on IE. CBroe thanks for your link

    – Kamuran Sönecek
    Sep 29 '15 at 7:22



















I want to find a solution without a plugin.

– Kamuran Sönecek
Sep 29 '15 at 7:13





I want to find a solution without a plugin.

– Kamuran Sönecek
Sep 29 '15 at 7:13




1




1





"On IE date format is MM-DD-YYYY" is a very broad statement. In what way is that "the date format"? The parameter to new Date(dateString) is documented to be in ISO-8601 or RFC2822 format, so I'd expect you to just be able to use dateSTR directly, without any messing around.

– Jon Skeet
Sep 29 '15 at 7:14





"On IE date format is MM-DD-YYYY" is a very broad statement. In what way is that "the date format"? The parameter to new Date(dateString) is documented to be in ISO-8601 or RFC2822 format, so I'd expect you to just be able to use dateSTR directly, without any messing around.

– Jon Skeet
Sep 29 '15 at 7:14




1




1





possible duplicate of Javascript Date() constructor doesn't work

– CBroe
Sep 29 '15 at 7:16





possible duplicate of Javascript Date() constructor doesn't work

– CBroe
Sep 29 '15 at 7:16













“I want to find a solution without a plugin” – well then write code that parses your input date format into its single parts yourself, and then feed them to the Date constructor version that takes single arguments for each one of them.

– CBroe
Sep 29 '15 at 7:18





“I want to find a solution without a plugin” – well then write code that parses your input date format into its single parts yourself, and then feed them to the Date constructor version that takes single arguments for each one of them.

– CBroe
Sep 29 '15 at 7:18













@JonSkeet when I use dateSTR directly, it does not work on IE. CBroe thanks for your link

– Kamuran Sönecek
Sep 29 '15 at 7:22







@JonSkeet when I use dateSTR directly, it does not work on IE. CBroe thanks for your link

– Kamuran Sönecek
Sep 29 '15 at 7:22














4 Answers
4






active

oldest

votes


















1














I only see a difference in the toLocaleDateString - NOTE: I am in Europe, so only FX is showing me what I want






    var dateSTR = "2015-09-29";
var date = new Date(dateSTR.replace(/-/g,"/"));

console.log("0:"+ new Date("2015/09/29"))
console.log("1:"+date)
date.setUTCHours(date.getHours());
console.log("2:"+date)
date.setUTCMinutes(date.getMinutes());
console.log("3:"+date);
console.log("4:"+date.toLocaleDateString());





Chrome:



0:Tue Sep 29 2015 00:00:00 GMT+0200 (W. Europe Daylight Time)
1:Tue Sep 29 2015 00:00:00 GMT+0200 (W. Europe Daylight Time)
2:Mon Sep 28 2015 02:00:00 GMT+0200 (W. Europe Daylight Time)
3:Mon Sep 28 2015 02:00:00 GMT+0200 (W. Europe Daylight Time)
4:9/28/2015


IE10:



0:Tue Sep 29 00:00:00 UTC+0200 2015 
1:Tue Sep 29 00:00:00 UTC+0200 2015
2:Mon Sep 28 02:00:00 UTC+0200 2015
3:Mon Sep 28 02:00:00 UTC+0200 2015
4:28 September 2015


FX:



"0:Tue Sep 29 2015 00:00:00 GMT+0200" 
"1:Tue Sep 29 2015 00:00:00 GMT+0200"
"2:Mon Sep 28 2015 02:00:00 GMT+0200"
"3:Mon Sep 28 2015 02:00:00 GMT+0200"
"4:28/09/2015"





share|improve this answer



















  • 1





    thank you @mplungjan it works perfectly

    – Kamuran Sönecek
    Sep 29 '15 at 7:49



















2














This code will return you the same date in IE, Chrome and FireFox:






var dateSTR = "2015-09-29";
var dateArr = dateSTR.split("-");
var theDate = new Date(dateArr[0], dateArr[1]-1, dateArr[2]);
alert(theDate);








share|improve this answer































    1














    It seems like you are trying to create a Date object from a predefined string (in your example: "2015-09-29").



    You can use the following:



    var dateSTR = "2015-09-29";
    var dateYear = 2015; // create this from dateSTR
    var dateMonth = 8; // create this from dateSTR (= month-1)
    var dateDay = 9; // create this from dateSTR


    and then use:



    var date = new Date(dateYear, dateMonth, dateDay);


    based on the explanation here: http://www.w3schools.com/jsref/jsref_obj_date.asp






    share|improve this answer
























    • thanks for your answer. the method is it available for date time?

      – Kamuran Sönecek
      Sep 29 '15 at 7:26











    • Yes, it can be used for setting date and time as well. Look at the reference link I attached

      – Miki Berkovich
      Sep 29 '15 at 7:45



















    1














    Split it like this



    var args = dateStr.split(/[-/]/); // Add any special characters if you need.


    And create date from it like this



    var d = new Date(args[0], args[1] - 1, args[2]);


    Thats it.






    share|improve this answer


























    • That will give October for the month if dateStr is "2015-09-29"...

      – Jon Skeet
      Sep 29 '15 at 7:30











    • @JonSkeet What now? For that small change, it does not deserve down vote

      – Exception
      Sep 29 '15 at 7:51






    • 1





      Why not? It confidently ("that's it") provided an answer that didn't work. I'd say that makes the answer "not useful", which is the tool tip for the downvote button... especially as optic had already provided similar but working code 10 minutes earlier. I've removed the downvote now that it's fixed, but I think it's entirely reasonable to downvote broken answers.

      – Jon Skeet
      Sep 29 '15 at 7:53











    • @JonSkeet Ok I agree with the comment you made if its matching question requirement, but the question is about logic to parse the values. And the focus of my answer was providing better parsing logic.. Later he can do whatever the operation he want to do with the date. And this question is not about date. Its about parsing the date.

      – Exception
      Sep 29 '15 at 11:05











    • And parsing the date to the wrong value is unhelpful, IMO. I'm pretty sure an implicit requirement of the question is that the Date obtained for "2015-09-29" is September 29th 2015, not January 1st 2001 for example...

      – Jon Skeet
      Sep 29 '15 at 11:11












    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f32837798%2fjavascript-new-date-cross-browser-format-problem%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    4 Answers
    4






    active

    oldest

    votes








    4 Answers
    4






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    I only see a difference in the toLocaleDateString - NOTE: I am in Europe, so only FX is showing me what I want






        var dateSTR = "2015-09-29";
    var date = new Date(dateSTR.replace(/-/g,"/"));

    console.log("0:"+ new Date("2015/09/29"))
    console.log("1:"+date)
    date.setUTCHours(date.getHours());
    console.log("2:"+date)
    date.setUTCMinutes(date.getMinutes());
    console.log("3:"+date);
    console.log("4:"+date.toLocaleDateString());





    Chrome:



    0:Tue Sep 29 2015 00:00:00 GMT+0200 (W. Europe Daylight Time)
    1:Tue Sep 29 2015 00:00:00 GMT+0200 (W. Europe Daylight Time)
    2:Mon Sep 28 2015 02:00:00 GMT+0200 (W. Europe Daylight Time)
    3:Mon Sep 28 2015 02:00:00 GMT+0200 (W. Europe Daylight Time)
    4:9/28/2015


    IE10:



    0:Tue Sep 29 00:00:00 UTC+0200 2015 
    1:Tue Sep 29 00:00:00 UTC+0200 2015
    2:Mon Sep 28 02:00:00 UTC+0200 2015
    3:Mon Sep 28 02:00:00 UTC+0200 2015
    4:28 September 2015


    FX:



    "0:Tue Sep 29 2015 00:00:00 GMT+0200" 
    "1:Tue Sep 29 2015 00:00:00 GMT+0200"
    "2:Mon Sep 28 2015 02:00:00 GMT+0200"
    "3:Mon Sep 28 2015 02:00:00 GMT+0200"
    "4:28/09/2015"





    share|improve this answer



















    • 1





      thank you @mplungjan it works perfectly

      – Kamuran Sönecek
      Sep 29 '15 at 7:49
















    1














    I only see a difference in the toLocaleDateString - NOTE: I am in Europe, so only FX is showing me what I want






        var dateSTR = "2015-09-29";
    var date = new Date(dateSTR.replace(/-/g,"/"));

    console.log("0:"+ new Date("2015/09/29"))
    console.log("1:"+date)
    date.setUTCHours(date.getHours());
    console.log("2:"+date)
    date.setUTCMinutes(date.getMinutes());
    console.log("3:"+date);
    console.log("4:"+date.toLocaleDateString());





    Chrome:



    0:Tue Sep 29 2015 00:00:00 GMT+0200 (W. Europe Daylight Time)
    1:Tue Sep 29 2015 00:00:00 GMT+0200 (W. Europe Daylight Time)
    2:Mon Sep 28 2015 02:00:00 GMT+0200 (W. Europe Daylight Time)
    3:Mon Sep 28 2015 02:00:00 GMT+0200 (W. Europe Daylight Time)
    4:9/28/2015


    IE10:



    0:Tue Sep 29 00:00:00 UTC+0200 2015 
    1:Tue Sep 29 00:00:00 UTC+0200 2015
    2:Mon Sep 28 02:00:00 UTC+0200 2015
    3:Mon Sep 28 02:00:00 UTC+0200 2015
    4:28 September 2015


    FX:



    "0:Tue Sep 29 2015 00:00:00 GMT+0200" 
    "1:Tue Sep 29 2015 00:00:00 GMT+0200"
    "2:Mon Sep 28 2015 02:00:00 GMT+0200"
    "3:Mon Sep 28 2015 02:00:00 GMT+0200"
    "4:28/09/2015"





    share|improve this answer



















    • 1





      thank you @mplungjan it works perfectly

      – Kamuran Sönecek
      Sep 29 '15 at 7:49














    1












    1








    1







    I only see a difference in the toLocaleDateString - NOTE: I am in Europe, so only FX is showing me what I want






        var dateSTR = "2015-09-29";
    var date = new Date(dateSTR.replace(/-/g,"/"));

    console.log("0:"+ new Date("2015/09/29"))
    console.log("1:"+date)
    date.setUTCHours(date.getHours());
    console.log("2:"+date)
    date.setUTCMinutes(date.getMinutes());
    console.log("3:"+date);
    console.log("4:"+date.toLocaleDateString());





    Chrome:



    0:Tue Sep 29 2015 00:00:00 GMT+0200 (W. Europe Daylight Time)
    1:Tue Sep 29 2015 00:00:00 GMT+0200 (W. Europe Daylight Time)
    2:Mon Sep 28 2015 02:00:00 GMT+0200 (W. Europe Daylight Time)
    3:Mon Sep 28 2015 02:00:00 GMT+0200 (W. Europe Daylight Time)
    4:9/28/2015


    IE10:



    0:Tue Sep 29 00:00:00 UTC+0200 2015 
    1:Tue Sep 29 00:00:00 UTC+0200 2015
    2:Mon Sep 28 02:00:00 UTC+0200 2015
    3:Mon Sep 28 02:00:00 UTC+0200 2015
    4:28 September 2015


    FX:



    "0:Tue Sep 29 2015 00:00:00 GMT+0200" 
    "1:Tue Sep 29 2015 00:00:00 GMT+0200"
    "2:Mon Sep 28 2015 02:00:00 GMT+0200"
    "3:Mon Sep 28 2015 02:00:00 GMT+0200"
    "4:28/09/2015"





    share|improve this answer













    I only see a difference in the toLocaleDateString - NOTE: I am in Europe, so only FX is showing me what I want






        var dateSTR = "2015-09-29";
    var date = new Date(dateSTR.replace(/-/g,"/"));

    console.log("0:"+ new Date("2015/09/29"))
    console.log("1:"+date)
    date.setUTCHours(date.getHours());
    console.log("2:"+date)
    date.setUTCMinutes(date.getMinutes());
    console.log("3:"+date);
    console.log("4:"+date.toLocaleDateString());





    Chrome:



    0:Tue Sep 29 2015 00:00:00 GMT+0200 (W. Europe Daylight Time)
    1:Tue Sep 29 2015 00:00:00 GMT+0200 (W. Europe Daylight Time)
    2:Mon Sep 28 2015 02:00:00 GMT+0200 (W. Europe Daylight Time)
    3:Mon Sep 28 2015 02:00:00 GMT+0200 (W. Europe Daylight Time)
    4:9/28/2015


    IE10:



    0:Tue Sep 29 00:00:00 UTC+0200 2015 
    1:Tue Sep 29 00:00:00 UTC+0200 2015
    2:Mon Sep 28 02:00:00 UTC+0200 2015
    3:Mon Sep 28 02:00:00 UTC+0200 2015
    4:28 September 2015


    FX:



    "0:Tue Sep 29 2015 00:00:00 GMT+0200" 
    "1:Tue Sep 29 2015 00:00:00 GMT+0200"
    "2:Mon Sep 28 2015 02:00:00 GMT+0200"
    "3:Mon Sep 28 2015 02:00:00 GMT+0200"
    "4:28/09/2015"





        var dateSTR = "2015-09-29";
    var date = new Date(dateSTR.replace(/-/g,"/"));

    console.log("0:"+ new Date("2015/09/29"))
    console.log("1:"+date)
    date.setUTCHours(date.getHours());
    console.log("2:"+date)
    date.setUTCMinutes(date.getMinutes());
    console.log("3:"+date);
    console.log("4:"+date.toLocaleDateString());





        var dateSTR = "2015-09-29";
    var date = new Date(dateSTR.replace(/-/g,"/"));

    console.log("0:"+ new Date("2015/09/29"))
    console.log("1:"+date)
    date.setUTCHours(date.getHours());
    console.log("2:"+date)
    date.setUTCMinutes(date.getMinutes());
    console.log("3:"+date);
    console.log("4:"+date.toLocaleDateString());






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Sep 29 '15 at 7:34









    mplungjanmplungjan

    90.5k22127185




    90.5k22127185








    • 1





      thank you @mplungjan it works perfectly

      – Kamuran Sönecek
      Sep 29 '15 at 7:49














    • 1





      thank you @mplungjan it works perfectly

      – Kamuran Sönecek
      Sep 29 '15 at 7:49








    1




    1





    thank you @mplungjan it works perfectly

    – Kamuran Sönecek
    Sep 29 '15 at 7:49





    thank you @mplungjan it works perfectly

    – Kamuran Sönecek
    Sep 29 '15 at 7:49













    2














    This code will return you the same date in IE, Chrome and FireFox:






    var dateSTR = "2015-09-29";
    var dateArr = dateSTR.split("-");
    var theDate = new Date(dateArr[0], dateArr[1]-1, dateArr[2]);
    alert(theDate);








    share|improve this answer




























      2














      This code will return you the same date in IE, Chrome and FireFox:






      var dateSTR = "2015-09-29";
      var dateArr = dateSTR.split("-");
      var theDate = new Date(dateArr[0], dateArr[1]-1, dateArr[2]);
      alert(theDate);








      share|improve this answer


























        2












        2








        2







        This code will return you the same date in IE, Chrome and FireFox:






        var dateSTR = "2015-09-29";
        var dateArr = dateSTR.split("-");
        var theDate = new Date(dateArr[0], dateArr[1]-1, dateArr[2]);
        alert(theDate);








        share|improve this answer













        This code will return you the same date in IE, Chrome and FireFox:






        var dateSTR = "2015-09-29";
        var dateArr = dateSTR.split("-");
        var theDate = new Date(dateArr[0], dateArr[1]-1, dateArr[2]);
        alert(theDate);








        var dateSTR = "2015-09-29";
        var dateArr = dateSTR.split("-");
        var theDate = new Date(dateArr[0], dateArr[1]-1, dateArr[2]);
        alert(theDate);





        var dateSTR = "2015-09-29";
        var dateArr = dateSTR.split("-");
        var theDate = new Date(dateArr[0], dateArr[1]-1, dateArr[2]);
        alert(theDate);






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Sep 29 '15 at 7:19









        opticoptic

        1479




        1479























            1














            It seems like you are trying to create a Date object from a predefined string (in your example: "2015-09-29").



            You can use the following:



            var dateSTR = "2015-09-29";
            var dateYear = 2015; // create this from dateSTR
            var dateMonth = 8; // create this from dateSTR (= month-1)
            var dateDay = 9; // create this from dateSTR


            and then use:



            var date = new Date(dateYear, dateMonth, dateDay);


            based on the explanation here: http://www.w3schools.com/jsref/jsref_obj_date.asp






            share|improve this answer
























            • thanks for your answer. the method is it available for date time?

              – Kamuran Sönecek
              Sep 29 '15 at 7:26











            • Yes, it can be used for setting date and time as well. Look at the reference link I attached

              – Miki Berkovich
              Sep 29 '15 at 7:45
















            1














            It seems like you are trying to create a Date object from a predefined string (in your example: "2015-09-29").



            You can use the following:



            var dateSTR = "2015-09-29";
            var dateYear = 2015; // create this from dateSTR
            var dateMonth = 8; // create this from dateSTR (= month-1)
            var dateDay = 9; // create this from dateSTR


            and then use:



            var date = new Date(dateYear, dateMonth, dateDay);


            based on the explanation here: http://www.w3schools.com/jsref/jsref_obj_date.asp






            share|improve this answer
























            • thanks for your answer. the method is it available for date time?

              – Kamuran Sönecek
              Sep 29 '15 at 7:26











            • Yes, it can be used for setting date and time as well. Look at the reference link I attached

              – Miki Berkovich
              Sep 29 '15 at 7:45














            1












            1








            1







            It seems like you are trying to create a Date object from a predefined string (in your example: "2015-09-29").



            You can use the following:



            var dateSTR = "2015-09-29";
            var dateYear = 2015; // create this from dateSTR
            var dateMonth = 8; // create this from dateSTR (= month-1)
            var dateDay = 9; // create this from dateSTR


            and then use:



            var date = new Date(dateYear, dateMonth, dateDay);


            based on the explanation here: http://www.w3schools.com/jsref/jsref_obj_date.asp






            share|improve this answer













            It seems like you are trying to create a Date object from a predefined string (in your example: "2015-09-29").



            You can use the following:



            var dateSTR = "2015-09-29";
            var dateYear = 2015; // create this from dateSTR
            var dateMonth = 8; // create this from dateSTR (= month-1)
            var dateDay = 9; // create this from dateSTR


            and then use:



            var date = new Date(dateYear, dateMonth, dateDay);


            based on the explanation here: http://www.w3schools.com/jsref/jsref_obj_date.asp







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Sep 29 '15 at 7:19









            Miki BerkovichMiki Berkovich

            366213




            366213













            • thanks for your answer. the method is it available for date time?

              – Kamuran Sönecek
              Sep 29 '15 at 7:26











            • Yes, it can be used for setting date and time as well. Look at the reference link I attached

              – Miki Berkovich
              Sep 29 '15 at 7:45



















            • thanks for your answer. the method is it available for date time?

              – Kamuran Sönecek
              Sep 29 '15 at 7:26











            • Yes, it can be used for setting date and time as well. Look at the reference link I attached

              – Miki Berkovich
              Sep 29 '15 at 7:45

















            thanks for your answer. the method is it available for date time?

            – Kamuran Sönecek
            Sep 29 '15 at 7:26





            thanks for your answer. the method is it available for date time?

            – Kamuran Sönecek
            Sep 29 '15 at 7:26













            Yes, it can be used for setting date and time as well. Look at the reference link I attached

            – Miki Berkovich
            Sep 29 '15 at 7:45





            Yes, it can be used for setting date and time as well. Look at the reference link I attached

            – Miki Berkovich
            Sep 29 '15 at 7:45











            1














            Split it like this



            var args = dateStr.split(/[-/]/); // Add any special characters if you need.


            And create date from it like this



            var d = new Date(args[0], args[1] - 1, args[2]);


            Thats it.






            share|improve this answer


























            • That will give October for the month if dateStr is "2015-09-29"...

              – Jon Skeet
              Sep 29 '15 at 7:30











            • @JonSkeet What now? For that small change, it does not deserve down vote

              – Exception
              Sep 29 '15 at 7:51






            • 1





              Why not? It confidently ("that's it") provided an answer that didn't work. I'd say that makes the answer "not useful", which is the tool tip for the downvote button... especially as optic had already provided similar but working code 10 minutes earlier. I've removed the downvote now that it's fixed, but I think it's entirely reasonable to downvote broken answers.

              – Jon Skeet
              Sep 29 '15 at 7:53











            • @JonSkeet Ok I agree with the comment you made if its matching question requirement, but the question is about logic to parse the values. And the focus of my answer was providing better parsing logic.. Later he can do whatever the operation he want to do with the date. And this question is not about date. Its about parsing the date.

              – Exception
              Sep 29 '15 at 11:05











            • And parsing the date to the wrong value is unhelpful, IMO. I'm pretty sure an implicit requirement of the question is that the Date obtained for "2015-09-29" is September 29th 2015, not January 1st 2001 for example...

              – Jon Skeet
              Sep 29 '15 at 11:11
















            1














            Split it like this



            var args = dateStr.split(/[-/]/); // Add any special characters if you need.


            And create date from it like this



            var d = new Date(args[0], args[1] - 1, args[2]);


            Thats it.






            share|improve this answer


























            • That will give October for the month if dateStr is "2015-09-29"...

              – Jon Skeet
              Sep 29 '15 at 7:30











            • @JonSkeet What now? For that small change, it does not deserve down vote

              – Exception
              Sep 29 '15 at 7:51






            • 1





              Why not? It confidently ("that's it") provided an answer that didn't work. I'd say that makes the answer "not useful", which is the tool tip for the downvote button... especially as optic had already provided similar but working code 10 minutes earlier. I've removed the downvote now that it's fixed, but I think it's entirely reasonable to downvote broken answers.

              – Jon Skeet
              Sep 29 '15 at 7:53











            • @JonSkeet Ok I agree with the comment you made if its matching question requirement, but the question is about logic to parse the values. And the focus of my answer was providing better parsing logic.. Later he can do whatever the operation he want to do with the date. And this question is not about date. Its about parsing the date.

              – Exception
              Sep 29 '15 at 11:05











            • And parsing the date to the wrong value is unhelpful, IMO. I'm pretty sure an implicit requirement of the question is that the Date obtained for "2015-09-29" is September 29th 2015, not January 1st 2001 for example...

              – Jon Skeet
              Sep 29 '15 at 11:11














            1












            1








            1







            Split it like this



            var args = dateStr.split(/[-/]/); // Add any special characters if you need.


            And create date from it like this



            var d = new Date(args[0], args[1] - 1, args[2]);


            Thats it.






            share|improve this answer















            Split it like this



            var args = dateStr.split(/[-/]/); // Add any special characters if you need.


            And create date from it like this



            var d = new Date(args[0], args[1] - 1, args[2]);


            Thats it.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Sep 29 '15 at 7:51

























            answered Sep 29 '15 at 7:29









            ExceptionException

            2,2421164118




            2,2421164118













            • That will give October for the month if dateStr is "2015-09-29"...

              – Jon Skeet
              Sep 29 '15 at 7:30











            • @JonSkeet What now? For that small change, it does not deserve down vote

              – Exception
              Sep 29 '15 at 7:51






            • 1





              Why not? It confidently ("that's it") provided an answer that didn't work. I'd say that makes the answer "not useful", which is the tool tip for the downvote button... especially as optic had already provided similar but working code 10 minutes earlier. I've removed the downvote now that it's fixed, but I think it's entirely reasonable to downvote broken answers.

              – Jon Skeet
              Sep 29 '15 at 7:53











            • @JonSkeet Ok I agree with the comment you made if its matching question requirement, but the question is about logic to parse the values. And the focus of my answer was providing better parsing logic.. Later he can do whatever the operation he want to do with the date. And this question is not about date. Its about parsing the date.

              – Exception
              Sep 29 '15 at 11:05











            • And parsing the date to the wrong value is unhelpful, IMO. I'm pretty sure an implicit requirement of the question is that the Date obtained for "2015-09-29" is September 29th 2015, not January 1st 2001 for example...

              – Jon Skeet
              Sep 29 '15 at 11:11



















            • That will give October for the month if dateStr is "2015-09-29"...

              – Jon Skeet
              Sep 29 '15 at 7:30











            • @JonSkeet What now? For that small change, it does not deserve down vote

              – Exception
              Sep 29 '15 at 7:51






            • 1





              Why not? It confidently ("that's it") provided an answer that didn't work. I'd say that makes the answer "not useful", which is the tool tip for the downvote button... especially as optic had already provided similar but working code 10 minutes earlier. I've removed the downvote now that it's fixed, but I think it's entirely reasonable to downvote broken answers.

              – Jon Skeet
              Sep 29 '15 at 7:53











            • @JonSkeet Ok I agree with the comment you made if its matching question requirement, but the question is about logic to parse the values. And the focus of my answer was providing better parsing logic.. Later he can do whatever the operation he want to do with the date. And this question is not about date. Its about parsing the date.

              – Exception
              Sep 29 '15 at 11:05











            • And parsing the date to the wrong value is unhelpful, IMO. I'm pretty sure an implicit requirement of the question is that the Date obtained for "2015-09-29" is September 29th 2015, not January 1st 2001 for example...

              – Jon Skeet
              Sep 29 '15 at 11:11

















            That will give October for the month if dateStr is "2015-09-29"...

            – Jon Skeet
            Sep 29 '15 at 7:30





            That will give October for the month if dateStr is "2015-09-29"...

            – Jon Skeet
            Sep 29 '15 at 7:30













            @JonSkeet What now? For that small change, it does not deserve down vote

            – Exception
            Sep 29 '15 at 7:51





            @JonSkeet What now? For that small change, it does not deserve down vote

            – Exception
            Sep 29 '15 at 7:51




            1




            1





            Why not? It confidently ("that's it") provided an answer that didn't work. I'd say that makes the answer "not useful", which is the tool tip for the downvote button... especially as optic had already provided similar but working code 10 minutes earlier. I've removed the downvote now that it's fixed, but I think it's entirely reasonable to downvote broken answers.

            – Jon Skeet
            Sep 29 '15 at 7:53





            Why not? It confidently ("that's it") provided an answer that didn't work. I'd say that makes the answer "not useful", which is the tool tip for the downvote button... especially as optic had already provided similar but working code 10 minutes earlier. I've removed the downvote now that it's fixed, but I think it's entirely reasonable to downvote broken answers.

            – Jon Skeet
            Sep 29 '15 at 7:53













            @JonSkeet Ok I agree with the comment you made if its matching question requirement, but the question is about logic to parse the values. And the focus of my answer was providing better parsing logic.. Later he can do whatever the operation he want to do with the date. And this question is not about date. Its about parsing the date.

            – Exception
            Sep 29 '15 at 11:05





            @JonSkeet Ok I agree with the comment you made if its matching question requirement, but the question is about logic to parse the values. And the focus of my answer was providing better parsing logic.. Later he can do whatever the operation he want to do with the date. And this question is not about date. Its about parsing the date.

            – Exception
            Sep 29 '15 at 11:05













            And parsing the date to the wrong value is unhelpful, IMO. I'm pretty sure an implicit requirement of the question is that the Date obtained for "2015-09-29" is September 29th 2015, not January 1st 2001 for example...

            – Jon Skeet
            Sep 29 '15 at 11:11





            And parsing the date to the wrong value is unhelpful, IMO. I'm pretty sure an implicit requirement of the question is that the Date obtained for "2015-09-29" is September 29th 2015, not January 1st 2001 for example...

            – Jon Skeet
            Sep 29 '15 at 11:11


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f32837798%2fjavascript-new-date-cross-browser-format-problem%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            'app-layout' is not a known element: how to share Component with different Modules

            android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

            WPF add header to Image with URL pettitions [duplicate]