1

In python how would I format the date as 1/1/1990?

dayToday = datetime.date(1990,1,1) print dayToday

This returns 1990年01月01日, but I want it to look like 1/1/1990. (Jan 1 1990)

asked Apr 19, 2013 at 4:25
3
  • that is how sane people store their dates. :) Commented Apr 19, 2013 at 4:28
  • 1
    @MattBall Not a duplicate if OP cares about leading 0. Commented Apr 19, 2013 at 4:42
  • @mgilson Combined, it's a duplicate, but I'm not sure it's applicable when neither of those answers OP separately on both how to format from a date, and to fix leading 0. Flag it if you feel like it. Commented Apr 19, 2013 at 4:51

4 Answers 4

1

Try to look into python datetime.strftime

dayToday = datetime.date(1990,1,1) 
print dayToday.strftime('%Y/%m/%d')
>>> 1990年01月01日
print dayToday.strftime('%Y/%b/%d')
>>> 1990/Jan/01
answered Apr 19, 2013 at 4:28
Sign up to request clarification or add additional context in comments.

Comments

0

Use the datetime.strftime function with an appropriate format string:

>>> now = datetime.datetime.now()
>>> print now.strftime('%Y/%m/%d')
2013年04月19日
answered Apr 19, 2013 at 4:28

1 Comment

Not the format OP asked for. Both regarding leading 0 and order of dates.
0

Others have showed how to get the output 1990年01月01日, but assuming you don't want the leading zeros in there, the only way that I know of to do it is to do the string formatting yourself:

>>> '{dt.year}/{dt.month}/{dt.day}'.format(dt = dt.datetime.now())
'2013/4/19'
answered Apr 19, 2013 at 4:31

Comments

0

With the correct format and a without leading 0:

>>> import datetime
>>> now = datetime.datetime.now()
>>> now.strftime("%-m/%-d/%Y")
'4/19/2013'

Reported to only work for Linux, but I haven't tested anything else personally.
Tested and working for 2.7.3 and 3.2.3 on Linux x64.

answered Apr 19, 2013 at 4:32

3 Comments

Are you sure? This gives me: '-m/-d/2013'. Maybe platform specific? (I'm using OS-X)
@mgilson It seems it's a Linux-only.
Tested on my linux machine and it works there. Which makes me feel good. I was trying stuff like this before I finally came up with the answer I did and none of it worked. Now I know why :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.