日期显示为datetime.date(YYYY, M, D)

huangapple go评论103阅读模式
英文:

Dates appearing in list as datetime.date(YYYY,M,D)

问题

I have a script that is printing the dates at a certain interval between a start date and an end date that is specified by the user. The dates are being printed in the format YYYY-MM-DD as in 2023-06-08. This is the format I like.

In addition to printing the dates, I added some code that is appending these dates to a list. However, when I run this code, the same dates are being entered into the list as [datetime.date(2023,6,1)]. Is there a way around this?

  1. sections = []
  2. #year month day format
  3. barstartdate = datetime.date(int(startyear),int(startmonth),int(startday))
  4. #year month day format
  5. barenddate = datetime.date(int(endyear),int(endmonth),int(endday))
  6. #delta time
  7. delta = datetime.timedelta(days=7)
  8. #iterate over range of dates
  9. while (barstartdate <= barenddate):
  10. print(barstartdate, end="\n")
  11. sections.append(barstartdate)
  12. barstartdate += delta
  13. print(sections)

Output:

  1. 2023-06-01
  2. 2023-06-08
  3. [datetime.date(2023, 6, 1), datetime.date(2023, 6, 8)]

For reference, the start date and stop date in this case was 2023-06-01 and 2023-06-14. The last bit of code shown is the output I am getting in my console. The first two dates are appearing as I want them to, but the list has an odd format. How do I fix this?

英文:

I have a script that is printing the dates at a certain interval between a start date and an end date that is specified by the user. The dates are being printed in the format YYYY-MM-DD as in 2023-06-08. This is the format I like.

In addition to printing the dates, I added some code that is appending these dates to a list. However, when I run this code, the same dates are being entered into the list as [datetime.date(2023,6,1)]. Is there a way around this?

  1. sections = []
  2. #year month day format
  3. barstartdate = datetime.date(int(startyear),int(startmonth),int(startday))
  4. #year month day format
  5. barenddate = datetime.date(int(endyear),int(endmonth),int(endday))
  6. #delta time
  7. delta = datetime.timedelta(days=7)
  8. #iterate over range of dates
  9. while (barstartdate &lt;= barenddate):
  10. print(barstartdate, end=&quot;\n&quot;)
  11. sections.append(barstartdate)
  12. barstartdate += delta
  13. print(sections)

Output:

  1. 2023-06-01
  2. 2023-06-08
  3. [datetime.date(2023, 6, 1), datetime.date(2023, 6, 8)]

For reference, the start date and stop date in this case was 2023-06-01 and 2023-06-14. The last bit of code shown is the output I am getting in my console. The first two dates are appearing as I want them to, but the list has an odd format. How do I fix this?

答案1

得分: 2

When you print an object it will convert the object to a string using the str() built-in. However, when you print a list, the objects inside the list will be converted using repr().

For many classes str() and repr() will return the same string, but not for date objects:

  1. &gt;&gt;&gt; str(datetime.date.today())
  2. &#39;2023-06-15&#39;
  3. &gt;&gt;&gt; repr(datetime.date.today())
  4. &#39;datetime.date(2023, 6, 15)&#39;

What you are seeing in the list is the string representation of a datetime.date instance. You need to convert them to strings using .strftime(&quot;%Y-%m-%d&quot;) or, in your case, using the shortcut method .isoformat().

Try replacing

  1. sections.append(barstartdate)

with

  1. sections.append(barstartdate.isoformat())
英文:

When you print an object it will convert the object to a string using the str() built-in. However, when you print a list, the objects inside the list will be converted using repr().

For many classes str() and repr() will return the same string, but not for date objects:

  1. &gt;&gt;&gt; str(datetime.date.today())
  2. &#39;2023-06-15&#39;
  3. &gt;&gt;&gt; repr(datetime.date.today())
  4. &#39;datetime.date(2023, 6, 15)&#39;

What you are seeing in the list is the string representation of a datetime.date instance. You need to convert them to strings using .strftime(&quot;%Y-%m-%d&quot;) or, in your case, using the shortcut method .isoformat().

Try replacing

  1. sections.append(barstartdate)

with

  1. sections.append(barstartdate.isoformat())

Additi

huangapple
  • 本文由 发表于 2023年6月15日 09:40:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76478549.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定