0

I have a Python2.7 script with a variable date= "21/05/2019" that works fine. I need the date variable to be datetime.now - 60 days.

However the output must be like time.struct_time(tm_year=2019, tm_mon=5, tm_mday=28, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=1, tm_yday=148, tm_isdst=-1)

datetime.datetime.now() with strptime won't work.

import datetime, time
from datetime import timedelta

N = 60
#pre_create_date = datetime.datetime.today() - datetime.timedelta(days=N)
pre_create_date = "28/04/2019"

# Sort out date format
pre_create_date = time.strptime(pre_create_date, "%d/%m/%Y")

print pre_create_date

I expect to be able to use datetime.datetime.today() - datetime.timedelta(days=N) but the output to be a datetime object.

Edit:

It looks like I haven't explained myself very well. Sorry about that.

The script is:

import datetime, time
from datetime import timedelta

N = 60
#pre_create_date = datetime.datetime.today() - datetime.timedelta(days=N)
pre_create_date = "28/04/2019"

# Sort out date format
pre_create_date = time.strptime(pre_create_date, "%d/%m/%Y")

print pre_create_date

Output:

time.struct_time(tm_year=2019, tm_mon=4, tm_mday=28, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=118, tm_isdst=-1)

Script using datetime.now - datetime.timedelta:

import datetime, time
from datetime import timedelta

N = 60
pre_create_date = datetime.datetime.today() - datetime.timedelta(days=N)

print pre_create_date

Output:

2019-03-31 21:09:25.183459

Expected output:

time.struct_time(tm_year=2019, tm_mon=3, tm_mday=31, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=118, tm_isdst=-1)
6
  • The output of datetime.datetime.today() - datetime.timedelta(days=N) is datetime.datetime already. You don't need to use strptime on it. Am I missing something? Commented May 30, 2019 at 7:14
  • The output is a datetime object. You are just seeing the string representation of the object when you print it. Commented May 30, 2019 at 7:14
  • @HenryYik The output is not a datetime object. Rather it is a time.struct_time object. Notice, in the second last line we are calling time.strptime() rather then datetime.strptime() this results in a time object rather then a datetime object Commented May 30, 2019 at 7:52
  • I was referring to OP's last sentence regarding datetime.datetime.today() - datetime.timedelta(days=N). Commented May 30, 2019 at 7:53
  • The question is a little bit ambiguous, in the second paragraph OP states that he want the output format to be time.struct_time() which is of time object, and in the last line, states that he wants a datetime object? Both lines are contradicting each other Commented May 30, 2019 at 7:57

3 Answers 3

1

When using pre_create_date = time.strptime(pre_create_date, "%d/%m/%Y")

You are creating a time.struct_time object.

Use datetime.datetime.strptime("28/04/2019", "%d/%m/%Y") instead and I think you'll get what you aim for

Sign up to request clarification or add additional context in comments.

Comments

0

Try:-

import time
import datetime


print(time.strftime("%Y-%m-%d %H:%M:%S %w/%j"))

pre_create_date = (datetime.datetime.today() - datetime.timedelta(60)).strftime("%Y-%m-%d %H:%M:%S %w/%j")

print(pre_create_date)

OUTPUT:-

2019-05-30 12:59:27 4/150
2019-03-31 12:59:27 0/090

WORKING:-

The above code first prints out the current Timestamp(the format is the one you asked for). And then outputs the 60 days early Timestamp from current date (today - 60).

Just used strftime() to get our desired attributes off the current date.

EXPLANATION OF CODES USED IN strftime():-

%Y     Year with century as a decimal number.
%m     Month as a zero-padded decimal number.
%d     Day of the month as a zero-padded decimal.

%H     Hour (24-hour clock) as a zero-padded decimal number.
%M     Minute as a zero-padded decimal number.
%S     Second as a zero-padded decimal number.

%w     Week number of the year (Monday as the first day of the week). All days in a new year preceding the first Monday are considered to be in week 0.
%j     Day of the year as a zero-padded decimal number.

NOTE:- After passing the dates via strftime() the output will be of datatype string. If you want the output to be a datetime object then use:-

datetimeobj_1 = datetime.datetime.strptime(pre_create_date, "%Y-%m-%d %H:%M:%S %w/%j")
print(type(datetimeobj_1))

OUTPUT:-

<class 'datetime.datetime'>

If you want the output to be a 'time.struct_time' object then use:-

datetimeobj_1 = time.strptime(pre_create_date, "%Y-%m-%d %H:%M:%S %w/%j")
print(type(datetimeobj_1))

OUTPUT:-

<class 'time.struct_time'>

1 Comment

This fixed my problem. Thanks! time.strptime(pre_create_date, "%Y-%m-%d %H:%M:%S")
0

you have syntax problem, change print pre_create_date to print (pre_create_date) to start with and you are assigning a string to pre_create_date try to get it in date format, and use pre_create_date = pre_create_date.strftime("%Y.%m.%d %H:%M:%S.%f") or any format as you you like,

good luck.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.