# [Tutor] datetime module problem

Hansen, Mike Mike.Hansen at atmel.com
Thu Apr 17 22:29:29 CEST 2008

```

> -----Original Message-----
> From: tutor-bounces at python.org
> [mailto:tutor-bounces at python.org] On Behalf Of Dick Moores
> Sent: Thursday, April 17, 2008 2:21 PM
> To: Python Tutor List
> Subject: Re: [Tutor] datetime module problem
>
> At 06:29 AM 4/17/2008, Kent Johnson wrote:
> >Dick Moores wrote:
> >
> >>>You could either create n with hours=minutes=0, or round the
> >>>difference up to the next whole number of days.
> >>I have no idea how to implement either of your suggestions as to
> >>how to eliminate it. Could you please spell them both out?
> >
> >1.
> >
> >In [17]: n=datetime.today()
> >In [18]: n=datetime(n.year, n.month, n.day)
> >In [19]: n
> >Out[19]: datetime.datetime(2008, 4, 17, 0, 0)
> >
> >2.
> >
> >In [20]: diff=y-n
> >In [21]: diff
> >Out[21]: datetime.timedelta(0, 57464, 721369)
> >In [22]: days = diff.days
> >In [23]: if diff.seconds or diff.microseconds:
> >    ....:     days += 1
> >    ....:
> >    ....:
> >In [24]: days
> >Out[24]: 1
>
> Thanks, Kent. So here's where I am now:
> <http://py77.python.pastebin.com/f5da44111>
>
> The script calculates correctly, but note the output, lines 34, 39,
> 49, 53. Please show me how to print these in the form 4/17/2007. I've
> tried everything I could think of.
>
> Dick
>

In [23]: import datetime

In [24]: x = datetime.datetime.now()

In [25]: x
Out[25]: datetime.datetime(2008, 4, 17, 14, 24, 18, 447000)

In [26]: x.month
Out[26]: 4

In [27]: x.day
Out[27]: 17

In [28]: x.year
Out[28]: 2008

In [30]: print "%s/%s/%s" %(x.month, x.day, x.year, )
4/17/2008

Does that help? There's probably another way to do it, but this seems to
work.

Mike
```