how to generate today's date in YYYY-MM-DD format '%Y-%m-%d' using Python3












-1














it is possible to generate YYYY-MM-DD 00:00:00 with the below code. However, this is pretty ugly.



Is there a more elegant way?



Thanks



# calculate current date in format YYYY-MM-DD (ugly hack)
strToday = str(datetime.datetime.today()).split()[0]
dateToday = datetime.datetime.strptime(strToday, '%Y-%m-%d')









share|improve this question


















  • 1




    What about it is ugly?
    – Scott Hunter
    Nov 23 at 0:22










  • That looks fine? You could always use from datetime import datetime instead of import datetime. Then you only need datetime instead of datetime.datetime.
    – RoadRunner
    Nov 23 at 0:55








  • 1




    I disagree with the comments above: formatting into a string just to parse it back out is indeed an ugly hack and a bad idea. The datetime constructor can take arbitrary fields, or you can use the replace() method to replace arbitrary fields, either of which are better than working with strings like this.
    – Daniel Pryden
    Nov 23 at 2:30






  • 1




    Use strftime instead, datetime.today().strftime('%Y-%m-%d')
    – stovfl
    Nov 23 at 9:28










  • "Today's date", as in the question title, is ambiguous. Do you want to end up with a datetime object or with a string?
    – usr2564301
    Nov 23 at 17:13
















-1














it is possible to generate YYYY-MM-DD 00:00:00 with the below code. However, this is pretty ugly.



Is there a more elegant way?



Thanks



# calculate current date in format YYYY-MM-DD (ugly hack)
strToday = str(datetime.datetime.today()).split()[0]
dateToday = datetime.datetime.strptime(strToday, '%Y-%m-%d')









share|improve this question


















  • 1




    What about it is ugly?
    – Scott Hunter
    Nov 23 at 0:22










  • That looks fine? You could always use from datetime import datetime instead of import datetime. Then you only need datetime instead of datetime.datetime.
    – RoadRunner
    Nov 23 at 0:55








  • 1




    I disagree with the comments above: formatting into a string just to parse it back out is indeed an ugly hack and a bad idea. The datetime constructor can take arbitrary fields, or you can use the replace() method to replace arbitrary fields, either of which are better than working with strings like this.
    – Daniel Pryden
    Nov 23 at 2:30






  • 1




    Use strftime instead, datetime.today().strftime('%Y-%m-%d')
    – stovfl
    Nov 23 at 9:28










  • "Today's date", as in the question title, is ambiguous. Do you want to end up with a datetime object or with a string?
    – usr2564301
    Nov 23 at 17:13














-1












-1








-1


0





it is possible to generate YYYY-MM-DD 00:00:00 with the below code. However, this is pretty ugly.



Is there a more elegant way?



Thanks



# calculate current date in format YYYY-MM-DD (ugly hack)
strToday = str(datetime.datetime.today()).split()[0]
dateToday = datetime.datetime.strptime(strToday, '%Y-%m-%d')









share|improve this question













it is possible to generate YYYY-MM-DD 00:00:00 with the below code. However, this is pretty ugly.



Is there a more elegant way?



Thanks



# calculate current date in format YYYY-MM-DD (ugly hack)
strToday = str(datetime.datetime.today()).split()[0]
dateToday = datetime.datetime.strptime(strToday, '%Y-%m-%d')






python datetime






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 at 0:21









WakeSurfin1

62




62








  • 1




    What about it is ugly?
    – Scott Hunter
    Nov 23 at 0:22










  • That looks fine? You could always use from datetime import datetime instead of import datetime. Then you only need datetime instead of datetime.datetime.
    – RoadRunner
    Nov 23 at 0:55








  • 1




    I disagree with the comments above: formatting into a string just to parse it back out is indeed an ugly hack and a bad idea. The datetime constructor can take arbitrary fields, or you can use the replace() method to replace arbitrary fields, either of which are better than working with strings like this.
    – Daniel Pryden
    Nov 23 at 2:30






  • 1




    Use strftime instead, datetime.today().strftime('%Y-%m-%d')
    – stovfl
    Nov 23 at 9:28










  • "Today's date", as in the question title, is ambiguous. Do you want to end up with a datetime object or with a string?
    – usr2564301
    Nov 23 at 17:13














  • 1




    What about it is ugly?
    – Scott Hunter
    Nov 23 at 0:22










  • That looks fine? You could always use from datetime import datetime instead of import datetime. Then you only need datetime instead of datetime.datetime.
    – RoadRunner
    Nov 23 at 0:55








  • 1




    I disagree with the comments above: formatting into a string just to parse it back out is indeed an ugly hack and a bad idea. The datetime constructor can take arbitrary fields, or you can use the replace() method to replace arbitrary fields, either of which are better than working with strings like this.
    – Daniel Pryden
    Nov 23 at 2:30






  • 1




    Use strftime instead, datetime.today().strftime('%Y-%m-%d')
    – stovfl
    Nov 23 at 9:28










  • "Today's date", as in the question title, is ambiguous. Do you want to end up with a datetime object or with a string?
    – usr2564301
    Nov 23 at 17:13








1




1




What about it is ugly?
– Scott Hunter
Nov 23 at 0:22




What about it is ugly?
– Scott Hunter
Nov 23 at 0:22












That looks fine? You could always use from datetime import datetime instead of import datetime. Then you only need datetime instead of datetime.datetime.
– RoadRunner
Nov 23 at 0:55






That looks fine? You could always use from datetime import datetime instead of import datetime. Then you only need datetime instead of datetime.datetime.
– RoadRunner
Nov 23 at 0:55






1




1




I disagree with the comments above: formatting into a string just to parse it back out is indeed an ugly hack and a bad idea. The datetime constructor can take arbitrary fields, or you can use the replace() method to replace arbitrary fields, either of which are better than working with strings like this.
– Daniel Pryden
Nov 23 at 2:30




I disagree with the comments above: formatting into a string just to parse it back out is indeed an ugly hack and a bad idea. The datetime constructor can take arbitrary fields, or you can use the replace() method to replace arbitrary fields, either of which are better than working with strings like this.
– Daniel Pryden
Nov 23 at 2:30




1




1




Use strftime instead, datetime.today().strftime('%Y-%m-%d')
– stovfl
Nov 23 at 9:28




Use strftime instead, datetime.today().strftime('%Y-%m-%d')
– stovfl
Nov 23 at 9:28












"Today's date", as in the question title, is ambiguous. Do you want to end up with a datetime object or with a string?
– usr2564301
Nov 23 at 17:13




"Today's date", as in the question title, is ambiguous. Do you want to end up with a datetime object or with a string?
– usr2564301
Nov 23 at 17:13












1 Answer
1






active

oldest

votes


















0














Thanks for the input. The code has been modified but still requires two steps. The hope was to accomplish this on one line.



# calculate current date in format YYYY-MM-DD 
strToday = datetime.today().strftime('%Y-%m-%d')
dateToday = datetime.strptime(strToday, '%Y-%m-%d')


Below demonstrates the data type conversion requirement for the above method.



## this approaches results to a string data type
>>> from datetime import datetime
>>> date = datetime.today().strftime('%Y-%m-%d')
>>> print(date)
'2018-11-23'
>>> print(type(date))
<class 'str'>

## second step is still required to convert to datetime data type
>>> dateobject = datetime.strptime(date, '%Y-%m-%d')
>>> print(type (dateobject))
<class 'datetime.datetime'>
>>> print(str(dateobject))
2018-11-23 00:00:00





share|improve this answer





















    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%2f53439341%2fhow-to-generate-todays-date-in-yyyy-mm-dd-format-y-m-d-using-python3%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Thanks for the input. The code has been modified but still requires two steps. The hope was to accomplish this on one line.



    # calculate current date in format YYYY-MM-DD 
    strToday = datetime.today().strftime('%Y-%m-%d')
    dateToday = datetime.strptime(strToday, '%Y-%m-%d')


    Below demonstrates the data type conversion requirement for the above method.



    ## this approaches results to a string data type
    >>> from datetime import datetime
    >>> date = datetime.today().strftime('%Y-%m-%d')
    >>> print(date)
    '2018-11-23'
    >>> print(type(date))
    <class 'str'>

    ## second step is still required to convert to datetime data type
    >>> dateobject = datetime.strptime(date, '%Y-%m-%d')
    >>> print(type (dateobject))
    <class 'datetime.datetime'>
    >>> print(str(dateobject))
    2018-11-23 00:00:00





    share|improve this answer


























      0














      Thanks for the input. The code has been modified but still requires two steps. The hope was to accomplish this on one line.



      # calculate current date in format YYYY-MM-DD 
      strToday = datetime.today().strftime('%Y-%m-%d')
      dateToday = datetime.strptime(strToday, '%Y-%m-%d')


      Below demonstrates the data type conversion requirement for the above method.



      ## this approaches results to a string data type
      >>> from datetime import datetime
      >>> date = datetime.today().strftime('%Y-%m-%d')
      >>> print(date)
      '2018-11-23'
      >>> print(type(date))
      <class 'str'>

      ## second step is still required to convert to datetime data type
      >>> dateobject = datetime.strptime(date, '%Y-%m-%d')
      >>> print(type (dateobject))
      <class 'datetime.datetime'>
      >>> print(str(dateobject))
      2018-11-23 00:00:00





      share|improve this answer
























        0












        0








        0






        Thanks for the input. The code has been modified but still requires two steps. The hope was to accomplish this on one line.



        # calculate current date in format YYYY-MM-DD 
        strToday = datetime.today().strftime('%Y-%m-%d')
        dateToday = datetime.strptime(strToday, '%Y-%m-%d')


        Below demonstrates the data type conversion requirement for the above method.



        ## this approaches results to a string data type
        >>> from datetime import datetime
        >>> date = datetime.today().strftime('%Y-%m-%d')
        >>> print(date)
        '2018-11-23'
        >>> print(type(date))
        <class 'str'>

        ## second step is still required to convert to datetime data type
        >>> dateobject = datetime.strptime(date, '%Y-%m-%d')
        >>> print(type (dateobject))
        <class 'datetime.datetime'>
        >>> print(str(dateobject))
        2018-11-23 00:00:00





        share|improve this answer












        Thanks for the input. The code has been modified but still requires two steps. The hope was to accomplish this on one line.



        # calculate current date in format YYYY-MM-DD 
        strToday = datetime.today().strftime('%Y-%m-%d')
        dateToday = datetime.strptime(strToday, '%Y-%m-%d')


        Below demonstrates the data type conversion requirement for the above method.



        ## this approaches results to a string data type
        >>> from datetime import datetime
        >>> date = datetime.today().strftime('%Y-%m-%d')
        >>> print(date)
        '2018-11-23'
        >>> print(type(date))
        <class 'str'>

        ## second step is still required to convert to datetime data type
        >>> dateobject = datetime.strptime(date, '%Y-%m-%d')
        >>> print(type (dateobject))
        <class 'datetime.datetime'>
        >>> print(str(dateobject))
        2018-11-23 00:00:00






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 at 16:42









        WakeSurfin1

        62




        62






























            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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2f53439341%2fhow-to-generate-todays-date-in-yyyy-mm-dd-format-y-m-d-using-python3%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

            Contact image not getting when fetch all contact list from iPhone by CNContact

            count number of partitions of a set with n elements into k subsets

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks