0

How do I print the current date in the below format?

year/month/date

I have been using the below, but it is adding an extra space. I need it to look like this '2020/9/14' and not like '2020 / 9 / 14. Any ideas? Below is current code for the latter option.

str(todays_date.year)
str(todays_date.month)
str(todays_date.day)
dash = "/"
str(dash)

print(todays_date.year,dash,todays_date.month,dash,todays_date.day)
jordanm
  • 33,009
  • 7
  • 61
  • 76
mquiz
  • 1
  • 1
  • This question is slightly different than the one noted as being a duplicate. The duplicate both A) Puts leading zeros on one-digit date components, and 2) may not be addressing the real question here at all if the OP is asking about string formatting....not that that doesn't mean that this is a dup of some other existing question. – CryptoFool Sep 14 '20 at 18:22

2 Answers2

1

Like this:

from datetime import datetime

print(datetime.today().strftime('%Y/%-m/%-d'))

UPDATE: I added the hyphens in there, realizing that you didn't want the leading zero on a one-digit month or day.

Today's result:

2020/9/14

If you want to use the individual values from the date and do your own formatting, you can do this to get the same result:

print("{}{}{}{}{}".format(todays_date.year,dash,todays_date.month,dash,todays_date.day))
CryptoFool
  • 21,719
  • 5
  • 26
  • 44
  • #Complete the code below. We've gone ahead and created #variables for the current date and current time. Now, #we want to print date with the format year/month/day, #and the time with the format hour:minute:second. # from datetime import date import datetime todays_date = date.today() #Complete the line below to print today's date with the #form year/month/day. For example, January 15th, 2016 #would be 2016/1/15. – mquiz Sep 14 '20 at 18:18
  • Strange. This text talks about creating variables, but the only variable created in what you posted is the `dash` variable. There's no sign of where `todays_date` comes from. Those other four lines wrapping `str()` around existing quantities do nothing. – CryptoFool Sep 14 '20 at 18:28
  • year_as_string = str(todays_date.year) month_as_string = str(todays_date.month) day_as_string = str(todays_date.day) print(todays_date.year,"/",todays_date.month,"/",todays_date.day) this prints with the spaces – mquiz Sep 14 '20 at 18:39
-1

this:

date = '2020 / 09 /14'

print(date.replace(' ',''))

prints:

'2020/09/14'
aggis
  • 608
  • 4
  • 9
  • Their date appears to be inside of a datetime object, not inside of a string that looks like that. – jordanm Sep 14 '20 at 18:14
  • Also, the extra spaces the OP is referring to is comes from default argument for `separator` in the `print` function, not the actual `str` itself – Wondercricket Sep 14 '20 at 18:17
  • OP never mentioned that they were dealing with a datetime object. The question asked how to remove spaces from the string. quickest solution is to remove the spaces using the code I provided, and then converting to a datetime object from there. – aggis Sep 14 '20 at 18:28