Best way to get date on YYYYMMDD format

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
5
down vote

favorite
2












I'm looking for the best way to return a date on a YYYYMMDD format : i.e. : 20180106



Here is how I do it :



var x = new Date();
var y = x.getFullYear().toString();
var m = (x.getMonth() + 1).toString();
var d = x.getDate().toString();
(d.length == 1) && (d = '0' + d);
(m.length == 1) && (m = '0' + m);
var yyyymmdd = y + m + d;
alert(yyyymmdd);


Is it correct enough or can I achieve the same in a beter way ?



Thank you for your light.







share|improve this question

















  • 2




    By the way, we are already in 2018. You wrote 20170106. ;)
    – Roland Illig
    Jan 6 at 17:23










  • To avoid those magic constants, you could also do const components = [d.getFullYear(), d.getMonth() + 1, d.getDay()]; const paddings = [4, 2, 2]; components.map((component, i) => component.toString().padStart(paddings[i], '0')).join('');
    – le_m
    Jan 6 at 21:31










  • See also stackoverflow.com/questions/3552461/…
    – mkrieger1
    Jan 6 at 21:38










  • What do the fifth and sixth lines do? I'm learning JavaScript and I get the left part is a Boolean check but you're using "and" with code that sets a variable?
    – BruceWayne
    Jan 7 at 0:16







  • 1




    @BruceWayne if the boolean check is true, the right side statement will get executed (which here sets the variable), otherwise; if the left condition fails, it'll not execute.
    – hjpotter92
    Jan 7 at 6:27
















up vote
5
down vote

favorite
2












I'm looking for the best way to return a date on a YYYYMMDD format : i.e. : 20180106



Here is how I do it :



var x = new Date();
var y = x.getFullYear().toString();
var m = (x.getMonth() + 1).toString();
var d = x.getDate().toString();
(d.length == 1) && (d = '0' + d);
(m.length == 1) && (m = '0' + m);
var yyyymmdd = y + m + d;
alert(yyyymmdd);


Is it correct enough or can I achieve the same in a beter way ?



Thank you for your light.







share|improve this question

















  • 2




    By the way, we are already in 2018. You wrote 20170106. ;)
    – Roland Illig
    Jan 6 at 17:23










  • To avoid those magic constants, you could also do const components = [d.getFullYear(), d.getMonth() + 1, d.getDay()]; const paddings = [4, 2, 2]; components.map((component, i) => component.toString().padStart(paddings[i], '0')).join('');
    – le_m
    Jan 6 at 21:31










  • See also stackoverflow.com/questions/3552461/…
    – mkrieger1
    Jan 6 at 21:38










  • What do the fifth and sixth lines do? I'm learning JavaScript and I get the left part is a Boolean check but you're using "and" with code that sets a variable?
    – BruceWayne
    Jan 7 at 0:16







  • 1




    @BruceWayne if the boolean check is true, the right side statement will get executed (which here sets the variable), otherwise; if the left condition fails, it'll not execute.
    – hjpotter92
    Jan 7 at 6:27












up vote
5
down vote

favorite
2









up vote
5
down vote

favorite
2






2





I'm looking for the best way to return a date on a YYYYMMDD format : i.e. : 20180106



Here is how I do it :



var x = new Date();
var y = x.getFullYear().toString();
var m = (x.getMonth() + 1).toString();
var d = x.getDate().toString();
(d.length == 1) && (d = '0' + d);
(m.length == 1) && (m = '0' + m);
var yyyymmdd = y + m + d;
alert(yyyymmdd);


Is it correct enough or can I achieve the same in a beter way ?



Thank you for your light.







share|improve this question













I'm looking for the best way to return a date on a YYYYMMDD format : i.e. : 20180106



Here is how I do it :



var x = new Date();
var y = x.getFullYear().toString();
var m = (x.getMonth() + 1).toString();
var d = x.getDate().toString();
(d.length == 1) && (d = '0' + d);
(m.length == 1) && (m = '0' + m);
var yyyymmdd = y + m + d;
alert(yyyymmdd);


Is it correct enough or can I achieve the same in a beter way ?



Thank you for your light.









share|improve this question












share|improve this question




share|improve this question








edited Jan 6 at 21:05
























asked Jan 6 at 16:46







user157436














  • 2




    By the way, we are already in 2018. You wrote 20170106. ;)
    – Roland Illig
    Jan 6 at 17:23










  • To avoid those magic constants, you could also do const components = [d.getFullYear(), d.getMonth() + 1, d.getDay()]; const paddings = [4, 2, 2]; components.map((component, i) => component.toString().padStart(paddings[i], '0')).join('');
    – le_m
    Jan 6 at 21:31










  • See also stackoverflow.com/questions/3552461/…
    – mkrieger1
    Jan 6 at 21:38










  • What do the fifth and sixth lines do? I'm learning JavaScript and I get the left part is a Boolean check but you're using "and" with code that sets a variable?
    – BruceWayne
    Jan 7 at 0:16







  • 1




    @BruceWayne if the boolean check is true, the right side statement will get executed (which here sets the variable), otherwise; if the left condition fails, it'll not execute.
    – hjpotter92
    Jan 7 at 6:27












  • 2




    By the way, we are already in 2018. You wrote 20170106. ;)
    – Roland Illig
    Jan 6 at 17:23










  • To avoid those magic constants, you could also do const components = [d.getFullYear(), d.getMonth() + 1, d.getDay()]; const paddings = [4, 2, 2]; components.map((component, i) => component.toString().padStart(paddings[i], '0')).join('');
    – le_m
    Jan 6 at 21:31










  • See also stackoverflow.com/questions/3552461/…
    – mkrieger1
    Jan 6 at 21:38










  • What do the fifth and sixth lines do? I'm learning JavaScript and I get the left part is a Boolean check but you're using "and" with code that sets a variable?
    – BruceWayne
    Jan 7 at 0:16







  • 1




    @BruceWayne if the boolean check is true, the right side statement will get executed (which here sets the variable), otherwise; if the left condition fails, it'll not execute.
    – hjpotter92
    Jan 7 at 6:27







2




2




By the way, we are already in 2018. You wrote 20170106. ;)
– Roland Illig
Jan 6 at 17:23




By the way, we are already in 2018. You wrote 20170106. ;)
– Roland Illig
Jan 6 at 17:23












To avoid those magic constants, you could also do const components = [d.getFullYear(), d.getMonth() + 1, d.getDay()]; const paddings = [4, 2, 2]; components.map((component, i) => component.toString().padStart(paddings[i], '0')).join('');
– le_m
Jan 6 at 21:31




To avoid those magic constants, you could also do const components = [d.getFullYear(), d.getMonth() + 1, d.getDay()]; const paddings = [4, 2, 2]; components.map((component, i) => component.toString().padStart(paddings[i], '0')).join('');
– le_m
Jan 6 at 21:31












See also stackoverflow.com/questions/3552461/…
– mkrieger1
Jan 6 at 21:38




See also stackoverflow.com/questions/3552461/…
– mkrieger1
Jan 6 at 21:38












What do the fifth and sixth lines do? I'm learning JavaScript and I get the left part is a Boolean check but you're using "and" with code that sets a variable?
– BruceWayne
Jan 7 at 0:16





What do the fifth and sixth lines do? I'm learning JavaScript and I get the left part is a Boolean check but you're using "and" with code that sets a variable?
– BruceWayne
Jan 7 at 0:16





1




1




@BruceWayne if the boolean check is true, the right side statement will get executed (which here sets the variable), otherwise; if the left condition fails, it'll not execute.
– hjpotter92
Jan 7 at 6:27




@BruceWayne if the boolean check is true, the right side statement will get executed (which here sets the variable), otherwise; if the left condition fails, it'll not execute.
– hjpotter92
Jan 7 at 6:27










3 Answers
3






active

oldest

votes

















up vote
3
down vote



accepted










The code looks good and works well. Since it provides a useful bit of work, you should convert it into a function. Then you can copy that function to another program if you need so.



function yyyymmdd() 
var x = new Date();
var y = x.getFullYear().toString();
var m = (x.getMonth() + 1).toString();
var d = x.getDate().toString();
(d.length == 1) && (d = '0' + d);
(m.length == 1) && (m = '0' + m);
var yyyymmdd = y + m + d;
return yyyymmdd;



To make this code a little easier to read, you should rename x to now.



You could also omit the calls to toString(), which makes the code a little shorter. Plus, you should introduce the variables mm and dd, so that you don't reassign to the d and m variables. This is a generally useful pattern, because when stepping through the code you can always look at the variable definition to see how it was computed. This is not possible for variables that change their value during execution.



The modified code looks like this:



function yyyymmdd() 
var now = new Date();
var y = now.getFullYear();
var m = now.getMonth() + 1;
var d = now.getDate();
var mm = m < 10 ? '0' + m : m;
var dd = d < 10 ? '0' + d : d;
return '' + y + mm + dd;



Or, you could inline the last few lines:



function yyyymmdd() 
var now = new Date();
var y = now.getFullYear();
var m = now.getMonth() + 1;
var d = now.getDate();
return '' + y + (m < 10 ? '0' : '') + m + (d < 10 ? '0' : '') + d;



That last variant is harder to read though, therefore I prefer the previous one.



Another possibility is to define a helper function that produces a two-digit string:



function yyyymmdd() 
function twoDigit(n) return (n < 10 ? '0' : '') + n;

var now = new Date();
return '' + now.getFullYear() + twoDigit(now.getMonth() + 1) + twoDigit(now.getDate());






share|improve this answer























  • There is also padStart. m.padStart(2, '0')
    – Kruga
    Jan 9 at 11:41

















up vote
2
down vote













For getting the 0 padded date and month values, I generally follow this approach:



let d = ('0' + x.getDate()).substring(-2)


However, the Date objects have a .toISOString method, which returns the string in YYYY-MM-DDTHH:MM:SSZ format, which you can split on T and then replace - (or vice-versa):



formatted_date = (new Date()).toISOString().replace(/-/g, '').split('T')[0]
// same as (new Date()).toISOString().split('T')[0].replace(/-/g, '')





share|improve this answer

















  • 1




    Woops... This first chosen solution has just failed. A new Date() like Sun Jan 07 2018 00:26:00 GMT+0100 toISOString() gives 2018-01-06T23:26:00.445Z resulting in 20180106 instead of the expected 20180107
    – user157436
    Jan 7 at 0:55










  • toISOString always uses UTC time, and I have no idea why the API doesn't include a function like toLocalISOString, which would be quite useful.
    – Roland Illig
    Jan 7 at 15:54

















up vote
-3
down vote
















yyyymmdd=x=>(f=x=>(x<10&&'0')+x,x.getFullYear()+f(x.getMonth()+1)+f(x.getDate()));
alert(yyyymmdd(new Date));








share|improve this answer





















    Your Answer




    StackExchange.ifUsing("editor", function ()
    return StackExchange.using("mathjaxEditing", function ()
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
    );
    );
    , "mathjax-editing");

    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: "196"
    ;
    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',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );








     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f184459%2fbest-way-to-get-date-on-yyyymmdd-format%23new-answer', 'question_page');

    );

    Post as a guest





























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    3
    down vote



    accepted










    The code looks good and works well. Since it provides a useful bit of work, you should convert it into a function. Then you can copy that function to another program if you need so.



    function yyyymmdd() 
    var x = new Date();
    var y = x.getFullYear().toString();
    var m = (x.getMonth() + 1).toString();
    var d = x.getDate().toString();
    (d.length == 1) && (d = '0' + d);
    (m.length == 1) && (m = '0' + m);
    var yyyymmdd = y + m + d;
    return yyyymmdd;



    To make this code a little easier to read, you should rename x to now.



    You could also omit the calls to toString(), which makes the code a little shorter. Plus, you should introduce the variables mm and dd, so that you don't reassign to the d and m variables. This is a generally useful pattern, because when stepping through the code you can always look at the variable definition to see how it was computed. This is not possible for variables that change their value during execution.



    The modified code looks like this:



    function yyyymmdd() 
    var now = new Date();
    var y = now.getFullYear();
    var m = now.getMonth() + 1;
    var d = now.getDate();
    var mm = m < 10 ? '0' + m : m;
    var dd = d < 10 ? '0' + d : d;
    return '' + y + mm + dd;



    Or, you could inline the last few lines:



    function yyyymmdd() 
    var now = new Date();
    var y = now.getFullYear();
    var m = now.getMonth() + 1;
    var d = now.getDate();
    return '' + y + (m < 10 ? '0' : '') + m + (d < 10 ? '0' : '') + d;



    That last variant is harder to read though, therefore I prefer the previous one.



    Another possibility is to define a helper function that produces a two-digit string:



    function yyyymmdd() 
    function twoDigit(n) return (n < 10 ? '0' : '') + n;

    var now = new Date();
    return '' + now.getFullYear() + twoDigit(now.getMonth() + 1) + twoDigit(now.getDate());






    share|improve this answer























    • There is also padStart. m.padStart(2, '0')
      – Kruga
      Jan 9 at 11:41














    up vote
    3
    down vote



    accepted










    The code looks good and works well. Since it provides a useful bit of work, you should convert it into a function. Then you can copy that function to another program if you need so.



    function yyyymmdd() 
    var x = new Date();
    var y = x.getFullYear().toString();
    var m = (x.getMonth() + 1).toString();
    var d = x.getDate().toString();
    (d.length == 1) && (d = '0' + d);
    (m.length == 1) && (m = '0' + m);
    var yyyymmdd = y + m + d;
    return yyyymmdd;



    To make this code a little easier to read, you should rename x to now.



    You could also omit the calls to toString(), which makes the code a little shorter. Plus, you should introduce the variables mm and dd, so that you don't reassign to the d and m variables. This is a generally useful pattern, because when stepping through the code you can always look at the variable definition to see how it was computed. This is not possible for variables that change their value during execution.



    The modified code looks like this:



    function yyyymmdd() 
    var now = new Date();
    var y = now.getFullYear();
    var m = now.getMonth() + 1;
    var d = now.getDate();
    var mm = m < 10 ? '0' + m : m;
    var dd = d < 10 ? '0' + d : d;
    return '' + y + mm + dd;



    Or, you could inline the last few lines:



    function yyyymmdd() 
    var now = new Date();
    var y = now.getFullYear();
    var m = now.getMonth() + 1;
    var d = now.getDate();
    return '' + y + (m < 10 ? '0' : '') + m + (d < 10 ? '0' : '') + d;



    That last variant is harder to read though, therefore I prefer the previous one.



    Another possibility is to define a helper function that produces a two-digit string:



    function yyyymmdd() 
    function twoDigit(n) return (n < 10 ? '0' : '') + n;

    var now = new Date();
    return '' + now.getFullYear() + twoDigit(now.getMonth() + 1) + twoDigit(now.getDate());






    share|improve this answer























    • There is also padStart. m.padStart(2, '0')
      – Kruga
      Jan 9 at 11:41












    up vote
    3
    down vote



    accepted







    up vote
    3
    down vote



    accepted






    The code looks good and works well. Since it provides a useful bit of work, you should convert it into a function. Then you can copy that function to another program if you need so.



    function yyyymmdd() 
    var x = new Date();
    var y = x.getFullYear().toString();
    var m = (x.getMonth() + 1).toString();
    var d = x.getDate().toString();
    (d.length == 1) && (d = '0' + d);
    (m.length == 1) && (m = '0' + m);
    var yyyymmdd = y + m + d;
    return yyyymmdd;



    To make this code a little easier to read, you should rename x to now.



    You could also omit the calls to toString(), which makes the code a little shorter. Plus, you should introduce the variables mm and dd, so that you don't reassign to the d and m variables. This is a generally useful pattern, because when stepping through the code you can always look at the variable definition to see how it was computed. This is not possible for variables that change their value during execution.



    The modified code looks like this:



    function yyyymmdd() 
    var now = new Date();
    var y = now.getFullYear();
    var m = now.getMonth() + 1;
    var d = now.getDate();
    var mm = m < 10 ? '0' + m : m;
    var dd = d < 10 ? '0' + d : d;
    return '' + y + mm + dd;



    Or, you could inline the last few lines:



    function yyyymmdd() 
    var now = new Date();
    var y = now.getFullYear();
    var m = now.getMonth() + 1;
    var d = now.getDate();
    return '' + y + (m < 10 ? '0' : '') + m + (d < 10 ? '0' : '') + d;



    That last variant is harder to read though, therefore I prefer the previous one.



    Another possibility is to define a helper function that produces a two-digit string:



    function yyyymmdd() 
    function twoDigit(n) return (n < 10 ? '0' : '') + n;

    var now = new Date();
    return '' + now.getFullYear() + twoDigit(now.getMonth() + 1) + twoDigit(now.getDate());






    share|improve this answer















    The code looks good and works well. Since it provides a useful bit of work, you should convert it into a function. Then you can copy that function to another program if you need so.



    function yyyymmdd() 
    var x = new Date();
    var y = x.getFullYear().toString();
    var m = (x.getMonth() + 1).toString();
    var d = x.getDate().toString();
    (d.length == 1) && (d = '0' + d);
    (m.length == 1) && (m = '0' + m);
    var yyyymmdd = y + m + d;
    return yyyymmdd;



    To make this code a little easier to read, you should rename x to now.



    You could also omit the calls to toString(), which makes the code a little shorter. Plus, you should introduce the variables mm and dd, so that you don't reassign to the d and m variables. This is a generally useful pattern, because when stepping through the code you can always look at the variable definition to see how it was computed. This is not possible for variables that change their value during execution.



    The modified code looks like this:



    function yyyymmdd() 
    var now = new Date();
    var y = now.getFullYear();
    var m = now.getMonth() + 1;
    var d = now.getDate();
    var mm = m < 10 ? '0' + m : m;
    var dd = d < 10 ? '0' + d : d;
    return '' + y + mm + dd;



    Or, you could inline the last few lines:



    function yyyymmdd() 
    var now = new Date();
    var y = now.getFullYear();
    var m = now.getMonth() + 1;
    var d = now.getDate();
    return '' + y + (m < 10 ? '0' : '') + m + (d < 10 ? '0' : '') + d;



    That last variant is harder to read though, therefore I prefer the previous one.



    Another possibility is to define a helper function that produces a two-digit string:



    function yyyymmdd() 
    function twoDigit(n) return (n < 10 ? '0' : '') + n;

    var now = new Date();
    return '' + now.getFullYear() + twoDigit(now.getMonth() + 1) + twoDigit(now.getDate());







    share|improve this answer















    share|improve this answer



    share|improve this answer








    edited Jan 6 at 17:27


























    answered Jan 6 at 17:22









    Roland Illig

    10.4k11643




    10.4k11643











    • There is also padStart. m.padStart(2, '0')
      – Kruga
      Jan 9 at 11:41
















    • There is also padStart. m.padStart(2, '0')
      – Kruga
      Jan 9 at 11:41















    There is also padStart. m.padStart(2, '0')
    – Kruga
    Jan 9 at 11:41




    There is also padStart. m.padStart(2, '0')
    – Kruga
    Jan 9 at 11:41












    up vote
    2
    down vote













    For getting the 0 padded date and month values, I generally follow this approach:



    let d = ('0' + x.getDate()).substring(-2)


    However, the Date objects have a .toISOString method, which returns the string in YYYY-MM-DDTHH:MM:SSZ format, which you can split on T and then replace - (or vice-versa):



    formatted_date = (new Date()).toISOString().replace(/-/g, '').split('T')[0]
    // same as (new Date()).toISOString().split('T')[0].replace(/-/g, '')





    share|improve this answer

















    • 1




      Woops... This first chosen solution has just failed. A new Date() like Sun Jan 07 2018 00:26:00 GMT+0100 toISOString() gives 2018-01-06T23:26:00.445Z resulting in 20180106 instead of the expected 20180107
      – user157436
      Jan 7 at 0:55










    • toISOString always uses UTC time, and I have no idea why the API doesn't include a function like toLocalISOString, which would be quite useful.
      – Roland Illig
      Jan 7 at 15:54














    up vote
    2
    down vote













    For getting the 0 padded date and month values, I generally follow this approach:



    let d = ('0' + x.getDate()).substring(-2)


    However, the Date objects have a .toISOString method, which returns the string in YYYY-MM-DDTHH:MM:SSZ format, which you can split on T and then replace - (or vice-versa):



    formatted_date = (new Date()).toISOString().replace(/-/g, '').split('T')[0]
    // same as (new Date()).toISOString().split('T')[0].replace(/-/g, '')





    share|improve this answer

















    • 1




      Woops... This first chosen solution has just failed. A new Date() like Sun Jan 07 2018 00:26:00 GMT+0100 toISOString() gives 2018-01-06T23:26:00.445Z resulting in 20180106 instead of the expected 20180107
      – user157436
      Jan 7 at 0:55










    • toISOString always uses UTC time, and I have no idea why the API doesn't include a function like toLocalISOString, which would be quite useful.
      – Roland Illig
      Jan 7 at 15:54












    up vote
    2
    down vote










    up vote
    2
    down vote









    For getting the 0 padded date and month values, I generally follow this approach:



    let d = ('0' + x.getDate()).substring(-2)


    However, the Date objects have a .toISOString method, which returns the string in YYYY-MM-DDTHH:MM:SSZ format, which you can split on T and then replace - (or vice-versa):



    formatted_date = (new Date()).toISOString().replace(/-/g, '').split('T')[0]
    // same as (new Date()).toISOString().split('T')[0].replace(/-/g, '')





    share|improve this answer













    For getting the 0 padded date and month values, I generally follow this approach:



    let d = ('0' + x.getDate()).substring(-2)


    However, the Date objects have a .toISOString method, which returns the string in YYYY-MM-DDTHH:MM:SSZ format, which you can split on T and then replace - (or vice-versa):



    formatted_date = (new Date()).toISOString().replace(/-/g, '').split('T')[0]
    // same as (new Date()).toISOString().split('T')[0].replace(/-/g, '')






    share|improve this answer













    share|improve this answer



    share|improve this answer











    answered Jan 6 at 17:38









    hjpotter92

    4,98611539




    4,98611539







    • 1




      Woops... This first chosen solution has just failed. A new Date() like Sun Jan 07 2018 00:26:00 GMT+0100 toISOString() gives 2018-01-06T23:26:00.445Z resulting in 20180106 instead of the expected 20180107
      – user157436
      Jan 7 at 0:55










    • toISOString always uses UTC time, and I have no idea why the API doesn't include a function like toLocalISOString, which would be quite useful.
      – Roland Illig
      Jan 7 at 15:54












    • 1




      Woops... This first chosen solution has just failed. A new Date() like Sun Jan 07 2018 00:26:00 GMT+0100 toISOString() gives 2018-01-06T23:26:00.445Z resulting in 20180106 instead of the expected 20180107
      – user157436
      Jan 7 at 0:55










    • toISOString always uses UTC time, and I have no idea why the API doesn't include a function like toLocalISOString, which would be quite useful.
      – Roland Illig
      Jan 7 at 15:54







    1




    1




    Woops... This first chosen solution has just failed. A new Date() like Sun Jan 07 2018 00:26:00 GMT+0100 toISOString() gives 2018-01-06T23:26:00.445Z resulting in 20180106 instead of the expected 20180107
    – user157436
    Jan 7 at 0:55




    Woops... This first chosen solution has just failed. A new Date() like Sun Jan 07 2018 00:26:00 GMT+0100 toISOString() gives 2018-01-06T23:26:00.445Z resulting in 20180106 instead of the expected 20180107
    – user157436
    Jan 7 at 0:55












    toISOString always uses UTC time, and I have no idea why the API doesn't include a function like toLocalISOString, which would be quite useful.
    – Roland Illig
    Jan 7 at 15:54




    toISOString always uses UTC time, and I have no idea why the API doesn't include a function like toLocalISOString, which would be quite useful.
    – Roland Illig
    Jan 7 at 15:54










    up vote
    -3
    down vote
















    yyyymmdd=x=>(f=x=>(x<10&&'0')+x,x.getFullYear()+f(x.getMonth()+1)+f(x.getDate()));
    alert(yyyymmdd(new Date));








    share|improve this answer

























      up vote
      -3
      down vote
















      yyyymmdd=x=>(f=x=>(x<10&&'0')+x,x.getFullYear()+f(x.getMonth()+1)+f(x.getDate()));
      alert(yyyymmdd(new Date));








      share|improve this answer























        up vote
        -3
        down vote










        up vote
        -3
        down vote












        yyyymmdd=x=>(f=x=>(x<10&&'0')+x,x.getFullYear()+f(x.getMonth()+1)+f(x.getDate()));
        alert(yyyymmdd(new Date));








        share|improve this answer
















        yyyymmdd=x=>(f=x=>(x<10&&'0')+x,x.getFullYear()+f(x.getMonth()+1)+f(x.getDate()));
        alert(yyyymmdd(new Date));








        yyyymmdd=x=>(f=x=>(x<10&&'0')+x,x.getFullYear()+f(x.getMonth()+1)+f(x.getDate()));
        alert(yyyymmdd(new Date));





        yyyymmdd=x=>(f=x=>(x<10&&'0')+x,x.getFullYear()+f(x.getMonth()+1)+f(x.getDate()));
        alert(yyyymmdd(new Date));






        share|improve this answer













        share|improve this answer



        share|improve this answer











        answered Jan 13 at 21:09









        Eman

        1




        1






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f184459%2fbest-way-to-get-date-on-yyyymmdd-format%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            Greedy Best First Search implementation in Rust

            Function to Return a JSON Like Objects Using VBA Collections and Arrays

            C++11 CLH Lock Implementation