How to convert a list of dates into a list of strings
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
add a comment |
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
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-19is a string. Why do you say it isn't?
– John Gordon
Nov 22 '18 at 2:33
add a comment |
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
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
python
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-19is a string. Why do you say it isn't?
– John Gordon
Nov 22 '18 at 2:33
add a comment |
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-19is 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
add a comment |
2 Answers
2
active
oldest
votes
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)]
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
add a comment |
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"))
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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)]
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
add a comment |
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)]
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
add a comment |
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)]
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)]
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
add a comment |
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
add a comment |
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"))
add a comment |
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"))
add a comment |
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"))
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"))
answered Nov 22 '18 at 2:38
Dennis19901Dennis19901
533
533
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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-19is a string. Why do you say it isn't?– John Gordon
Nov 22 '18 at 2:33