Python Forum
how to convert and format a varable
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to convert and format a varable
#1
Hi
can any one pls help me with convering and formating the variable timeset so my cod can work.
it works only if i replace the timeset variable with the date.

import datetime
 
# to get today's date
today = datetime.date.today()
timeset = "2020, 5, 29" 
# code works when i put the date of timeset variable here
thedate = datetime.date(timeset)
 
# to get delta in days form today (this is an absolute value)
delta = today - thedate
 
print(f"The delta is {delta.days} days")


if delta.days > 0:
     print("dags")

elif delta.days < 365:
     print("inte dags")
Reply
#2
Couple things. Don't use commas in your timeset, and use a different function for the conversion. The following works:
import datetime
from datetime import datetime as dt
  
# to get today's date
today = datetime.date.today()
timeset = "2020 05 29" 
# code works when i put the date of timeset variable here
thedate = dt.strptime(timeset, '%Y %m %d').date()
  
# to get delta in days form today (this is an absolute value)
delta = today - thedate
  
print(f"The delta is {delta.days} days")
 
 
if delta.days > 0:
     print("dags")
 
elif delta.days < 365:
     print("inte dags")
Reply
#3
import datetime

today = datetime.date.today()

thedate = datetime.date(2020, 5, 29)

delta = today - thedate
  
print(f"The delta is {delta.days} days")
Output:
The delta is 730 days
Reply
#4
(May-29-2022, 12:15 PM)Larz60+ Wrote:
import datetime

today = datetime.date.today()

thedate = datetime.date(2020, 5, 29)

delta = today - thedate
  
print(f"The delta is {delta.days} days")
Output:
The delta is 730 days

Yes i know this was the only way i could make it work and this was how the code looked originally . I wrote it but mayby i did not make it clear. my bad. I need to use a variable with the date in this code its how this bit of code is going in to the bigger code.
Reply
#5
(May-29-2022, 11:57 AM)jefsummers Wrote: Couple things. Don't use commas in your timeset, and use a different function for the conversion. The following works:
import datetime
from datetime import datetime as dt
  
# to get today's date
today = datetime.date.today()
timeset = "2020 05 29" 
# code works when i put the date of timeset variable here
thedate = dt.strptime(timeset, '%Y %m %d').date()
  
# to get delta in days form today (this is an absolute value)
delta = today - thedate
  
print(f"The delta is {delta.days} days")
 
 
if delta.days > 0:
     print("dags")
 
elif delta.days < 365:
     print("inte dags")

Thanks a lot. works like a charm Big Grin
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Thumbs Up Convert word into pdf and copy table to outlook body in a prescribed format email2kmahe 1 763 Sep-22-2023, 02:33 PM
Last Post: carecavoador
  Convert Json to table format python_student 2 5,574 Sep-28-2022, 12:48 PM
Last Post: python_student
  Convert .xlsx to Format as Table bnadir55 0 896 Aug-11-2022, 06:39 AM
Last Post: bnadir55
  Convert Date to another format lonesoac0 2 1,680 Mar-17-2022, 11:26 AM
Last Post: DeaD_EyE
  Convert timedelta to specified format Planetary_Assault_Systems 3 3,212 Jun-27-2021, 01:46 PM
Last Post: snippsat
  How to convert dates in odd format to months lokhtar 2 2,250 Apr-17-2021, 11:54 AM
Last Post: lokhtar
  Convert email addresses to VCF format jehoshua 2 4,698 Mar-06-2021, 12:50 AM
Last Post: jehoshua
  Convert date integers (ex. 43831) to regular format Galven 2 2,643 Nov-15-2020, 11:38 PM
Last Post: bowlofred
  CPC File Format (Cartesian Perceptual Compression) - Can Python Convert / Handle Them PSKrieger 2 2,487 Nov-11-2020, 02:57 PM
Last Post: PSKrieger
  Convert SAS Dates Format in a loop MohanReddy 2 3,058 Apr-02-2019, 10:31 AM
Last Post: scidam

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020