CoolRunning
CoolRunning

Reputation: 13

Date differences with tuples representing dates

Using Python3, I have tuples representing dates, e.g. (2008, 11, 10). I would like to manipulate these as dates, e.g to find date differences (subtractions) between 2 such tuples with result in days. I have looked here for ways to convert tuples into dates but have had no success.

(I can convert them into strings and manipulate those, using things like strftime("%Y-%m-%d %H:%M:%S") but that seems a long way around)

The examples I see start with literals:

var = datetime.date(2008, 11, 10)

or from system values:

today = datetime.date.today()

but I need to start with:

myvariable = (2008, 11, 10)     # the value that it has already
var = datetime.date(myvariable) 

which gives:

TypeError: an integer is required (got type tuple)

I realise that this is a newbie question. I just need a little help.

Upvotes: 1

Views: 672

Answers (2)

vineeth560
vineeth560

Reputation: 89

I think the correct syntax should be datetime.date(year, month, day)

myvariable = (2008, 11, 10)
var = datetime.date(myvariable[0],myvariable[1],myvariable[2])

now var has datetime object. For Further methods you can refer this link

Upvotes: 1

zipa
zipa

Reputation: 27869

You need to unpack it:

myvariable = (2008, 11, 10)
var = datetime.date(*myvariable) 

Upvotes: 3

Related Questions