How to convert a list of dates into a list of strings












1















I am trying to have a list of dates printed out across a google spreadsheet, so far I have the following script, which works fine in getting a list of dates, but I need to know how to convert this list of dates into a list of strings..



def daterange(startdate, enddate):
r = (enddate+datetime.timedelta(days=1)-startdate).days
return [startdate+datetime.timedelta(days=i) for i in range(r)]


startdate = datetime.date(2018, 11, 19)
enddate = datetime.date(2018,11,25)
datelist = daterange(startdate, enddate)

print ([str(date) for date in datelist])


I would have thought that 'str(date) would have accomplished this but it is still a list of dates..I am not an experienced programmed so please explain simply.



** EDIT



I realized my error.. It was printing a string fine, but as far as I can tell I need the dates to be stored as strings first, and then to update the spreadsheet with a list of strings.. I actually didn't need to use the print function at all (and I haven't with other lists of strings I've worked with on this project)Can anyone show me how to convert and store these dates as strings?










share|improve this question

























  • You could use docs.python.org/2/library/…

    – Red Cricket
    Nov 22 '18 at 2:31













  • Possible duplicate of Convert datetime object to a String of date only in Python

    – Red Cricket
    Nov 22 '18 at 2:32











  • 2018-11-19 is a string. Why do you say it isn't?

    – John Gordon
    Nov 22 '18 at 2:33


















1















I am trying to have a list of dates printed out across a google spreadsheet, so far I have the following script, which works fine in getting a list of dates, but I need to know how to convert this list of dates into a list of strings..



def daterange(startdate, enddate):
r = (enddate+datetime.timedelta(days=1)-startdate).days
return [startdate+datetime.timedelta(days=i) for i in range(r)]


startdate = datetime.date(2018, 11, 19)
enddate = datetime.date(2018,11,25)
datelist = daterange(startdate, enddate)

print ([str(date) for date in datelist])


I would have thought that 'str(date) would have accomplished this but it is still a list of dates..I am not an experienced programmed so please explain simply.



** EDIT



I realized my error.. It was printing a string fine, but as far as I can tell I need the dates to be stored as strings first, and then to update the spreadsheet with a list of strings.. I actually didn't need to use the print function at all (and I haven't with other lists of strings I've worked with on this project)Can anyone show me how to convert and store these dates as strings?










share|improve this question

























  • You could use docs.python.org/2/library/…

    – Red Cricket
    Nov 22 '18 at 2:31













  • Possible duplicate of Convert datetime object to a String of date only in Python

    – Red Cricket
    Nov 22 '18 at 2:32











  • 2018-11-19 is a string. Why do you say it isn't?

    – John Gordon
    Nov 22 '18 at 2:33
















1












1








1








I am trying to have a list of dates printed out across a google spreadsheet, so far I have the following script, which works fine in getting a list of dates, but I need to know how to convert this list of dates into a list of strings..



def daterange(startdate, enddate):
r = (enddate+datetime.timedelta(days=1)-startdate).days
return [startdate+datetime.timedelta(days=i) for i in range(r)]


startdate = datetime.date(2018, 11, 19)
enddate = datetime.date(2018,11,25)
datelist = daterange(startdate, enddate)

print ([str(date) for date in datelist])


I would have thought that 'str(date) would have accomplished this but it is still a list of dates..I am not an experienced programmed so please explain simply.



** EDIT



I realized my error.. It was printing a string fine, but as far as I can tell I need the dates to be stored as strings first, and then to update the spreadsheet with a list of strings.. I actually didn't need to use the print function at all (and I haven't with other lists of strings I've worked with on this project)Can anyone show me how to convert and store these dates as strings?










share|improve this question
















I am trying to have a list of dates printed out across a google spreadsheet, so far I have the following script, which works fine in getting a list of dates, but I need to know how to convert this list of dates into a list of strings..



def daterange(startdate, enddate):
r = (enddate+datetime.timedelta(days=1)-startdate).days
return [startdate+datetime.timedelta(days=i) for i in range(r)]


startdate = datetime.date(2018, 11, 19)
enddate = datetime.date(2018,11,25)
datelist = daterange(startdate, enddate)

print ([str(date) for date in datelist])


I would have thought that 'str(date) would have accomplished this but it is still a list of dates..I am not an experienced programmed so please explain simply.



** EDIT



I realized my error.. It was printing a string fine, but as far as I can tell I need the dates to be stored as strings first, and then to update the spreadsheet with a list of strings.. I actually didn't need to use the print function at all (and I haven't with other lists of strings I've worked with on this project)Can anyone show me how to convert and store these dates as strings?







python






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 3:44









Spencer Wieczorek

17.5k43345




17.5k43345










asked Nov 22 '18 at 2:28









will27272will27272

62




62













  • You could use docs.python.org/2/library/…

    – Red Cricket
    Nov 22 '18 at 2:31













  • Possible duplicate of Convert datetime object to a String of date only in Python

    – Red Cricket
    Nov 22 '18 at 2:32











  • 2018-11-19 is a string. Why do you say it isn't?

    – John Gordon
    Nov 22 '18 at 2:33





















  • You could use docs.python.org/2/library/…

    – Red Cricket
    Nov 22 '18 at 2:31













  • Possible duplicate of Convert datetime object to a String of date only in Python

    – Red Cricket
    Nov 22 '18 at 2:32











  • 2018-11-19 is a string. Why do you say it isn't?

    – John Gordon
    Nov 22 '18 at 2:33



















You could use docs.python.org/2/library/…

– Red Cricket
Nov 22 '18 at 2:31







You could use docs.python.org/2/library/…

– Red Cricket
Nov 22 '18 at 2:31















Possible duplicate of Convert datetime object to a String of date only in Python

– Red Cricket
Nov 22 '18 at 2:32





Possible duplicate of Convert datetime object to a String of date only in Python

– Red Cricket
Nov 22 '18 at 2:32













2018-11-19 is a string. Why do you say it isn't?

– John Gordon
Nov 22 '18 at 2:33







2018-11-19 is a string. Why do you say it isn't?

– John Gordon
Nov 22 '18 at 2:33














2 Answers
2






active

oldest

votes


















3














This line is ok:



print ([str(date) for date in datelist])


These are other ways you could print out the datelist:



print ([date.strftime('%Y-%m-%d') for date in datelist])
print ([str(date) for date in datelist])
print (["%s" % date for date in datelist])
print ([date for date in datelist])
print (datelist)


The last two examples results in this output:



[datetime.date(2018, 11, 19), datetime.date(2018, 11, 20), datetime.date(2018, 11, 21), datetime.date(2018, 11, 22), datetime.date(2018, 11, 23), datetime.date(2018, 11, 24), datetime.date(2018, 11, 25)]





share|improve this answer


























  • I realised my error.. It was printing a string fine, but as far as I can tell I need the dates to be stored as strings first, and then to update the spreadsheet with a list of strings.. I actually didn't need to use the print function at all (and I haven't with other lists of strings I've worked with on this project)

    – will27272
    Nov 22 '18 at 3:01



















1














Take a look at https://docs.python.org/2/library/datetime.html#strftime-strptime-behavior



With this you can convert a date to your desired string format. Loop over your list again and turn them all into string variants.



for date in datelist:
print(data.strftime("%B %d, %Y"))





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%2f53423072%2fhow-to-convert-a-list-of-dates-into-a-list-of-strings%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    This line is ok:



    print ([str(date) for date in datelist])


    These are other ways you could print out the datelist:



    print ([date.strftime('%Y-%m-%d') for date in datelist])
    print ([str(date) for date in datelist])
    print (["%s" % date for date in datelist])
    print ([date for date in datelist])
    print (datelist)


    The last two examples results in this output:



    [datetime.date(2018, 11, 19), datetime.date(2018, 11, 20), datetime.date(2018, 11, 21), datetime.date(2018, 11, 22), datetime.date(2018, 11, 23), datetime.date(2018, 11, 24), datetime.date(2018, 11, 25)]





    share|improve this answer


























    • I realised my error.. It was printing a string fine, but as far as I can tell I need the dates to be stored as strings first, and then to update the spreadsheet with a list of strings.. I actually didn't need to use the print function at all (and I haven't with other lists of strings I've worked with on this project)

      – will27272
      Nov 22 '18 at 3:01
















    3














    This line is ok:



    print ([str(date) for date in datelist])


    These are other ways you could print out the datelist:



    print ([date.strftime('%Y-%m-%d') for date in datelist])
    print ([str(date) for date in datelist])
    print (["%s" % date for date in datelist])
    print ([date for date in datelist])
    print (datelist)


    The last two examples results in this output:



    [datetime.date(2018, 11, 19), datetime.date(2018, 11, 20), datetime.date(2018, 11, 21), datetime.date(2018, 11, 22), datetime.date(2018, 11, 23), datetime.date(2018, 11, 24), datetime.date(2018, 11, 25)]





    share|improve this answer


























    • I realised my error.. It was printing a string fine, but as far as I can tell I need the dates to be stored as strings first, and then to update the spreadsheet with a list of strings.. I actually didn't need to use the print function at all (and I haven't with other lists of strings I've worked with on this project)

      – will27272
      Nov 22 '18 at 3:01














    3












    3








    3







    This line is ok:



    print ([str(date) for date in datelist])


    These are other ways you could print out the datelist:



    print ([date.strftime('%Y-%m-%d') for date in datelist])
    print ([str(date) for date in datelist])
    print (["%s" % date for date in datelist])
    print ([date for date in datelist])
    print (datelist)


    The last two examples results in this output:



    [datetime.date(2018, 11, 19), datetime.date(2018, 11, 20), datetime.date(2018, 11, 21), datetime.date(2018, 11, 22), datetime.date(2018, 11, 23), datetime.date(2018, 11, 24), datetime.date(2018, 11, 25)]





    share|improve this answer















    This line is ok:



    print ([str(date) for date in datelist])


    These are other ways you could print out the datelist:



    print ([date.strftime('%Y-%m-%d') for date in datelist])
    print ([str(date) for date in datelist])
    print (["%s" % date for date in datelist])
    print ([date for date in datelist])
    print (datelist)


    The last two examples results in this output:



    [datetime.date(2018, 11, 19), datetime.date(2018, 11, 20), datetime.date(2018, 11, 21), datetime.date(2018, 11, 22), datetime.date(2018, 11, 23), datetime.date(2018, 11, 24), datetime.date(2018, 11, 25)]






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 22 '18 at 3:18

























    answered Nov 22 '18 at 2:38









    Red CricketRed Cricket

    4,454103386




    4,454103386













    • I realised my error.. It was printing a string fine, but as far as I can tell I need the dates to be stored as strings first, and then to update the spreadsheet with a list of strings.. I actually didn't need to use the print function at all (and I haven't with other lists of strings I've worked with on this project)

      – will27272
      Nov 22 '18 at 3:01



















    • I realised my error.. It was printing a string fine, but as far as I can tell I need the dates to be stored as strings first, and then to update the spreadsheet with a list of strings.. I actually didn't need to use the print function at all (and I haven't with other lists of strings I've worked with on this project)

      – will27272
      Nov 22 '18 at 3:01

















    I realised my error.. It was printing a string fine, but as far as I can tell I need the dates to be stored as strings first, and then to update the spreadsheet with a list of strings.. I actually didn't need to use the print function at all (and I haven't with other lists of strings I've worked with on this project)

    – will27272
    Nov 22 '18 at 3:01





    I realised my error.. It was printing a string fine, but as far as I can tell I need the dates to be stored as strings first, and then to update the spreadsheet with a list of strings.. I actually didn't need to use the print function at all (and I haven't with other lists of strings I've worked with on this project)

    – will27272
    Nov 22 '18 at 3:01













    1














    Take a look at https://docs.python.org/2/library/datetime.html#strftime-strptime-behavior



    With this you can convert a date to your desired string format. Loop over your list again and turn them all into string variants.



    for date in datelist:
    print(data.strftime("%B %d, %Y"))





    share|improve this answer




























      1














      Take a look at https://docs.python.org/2/library/datetime.html#strftime-strptime-behavior



      With this you can convert a date to your desired string format. Loop over your list again and turn them all into string variants.



      for date in datelist:
      print(data.strftime("%B %d, %Y"))





      share|improve this answer


























        1












        1








        1







        Take a look at https://docs.python.org/2/library/datetime.html#strftime-strptime-behavior



        With this you can convert a date to your desired string format. Loop over your list again and turn them all into string variants.



        for date in datelist:
        print(data.strftime("%B %d, %Y"))





        share|improve this answer













        Take a look at https://docs.python.org/2/library/datetime.html#strftime-strptime-behavior



        With this you can convert a date to your desired string format. Loop over your list again and turn them all into string variants.



        for date in datelist:
        print(data.strftime("%B %d, %Y"))






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 '18 at 2:38









        Dennis19901Dennis19901

        533




        533






























            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%2f53423072%2fhow-to-convert-a-list-of-dates-into-a-list-of-strings%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

            Paul Cézanne

            UIScrollView CustomStickyHeader Resize height generates problems when scroll is too fast

            Angular material date-picker (MatDatepicker) auto completes the date on focus out