how to generate today's date in YYYY-MM-DD format '%Y-%m-%d' using Python3
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
add a comment |
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
1
What about it is ugly?
– Scott Hunter
Nov 23 at 0:22
That looks fine? You could always usefrom datetime import datetime
instead ofimport datetime
. Then you only needdatetime
instead ofdatetime.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. Thedatetime
constructor can take arbitrary fields, or you can use thereplace()
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
add a comment |
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
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
python datetime
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 usefrom datetime import datetime
instead ofimport datetime
. Then you only needdatetime
instead ofdatetime.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. Thedatetime
constructor can take arbitrary fields, or you can use thereplace()
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
add a comment |
1
What about it is ugly?
– Scott Hunter
Nov 23 at 0:22
That looks fine? You could always usefrom datetime import datetime
instead ofimport datetime
. Then you only needdatetime
instead ofdatetime.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. Thedatetime
constructor can take arbitrary fields, or you can use thereplace()
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
add a comment |
1 Answer
1
active
oldest
votes
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
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%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
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
add a comment |
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
add a comment |
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
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
answered Nov 23 at 16:42
WakeSurfin1
62
62
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.
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.
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%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
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
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 ofimport datetime
. Then you only needdatetime
instead ofdatetime.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 thereplace()
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