Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date formatting using python

To get date I use this block:

currentDate = date.today()
today = currentDate.strftime('%m/%d/%Y')

It returns me this format 12/22/2014 or 01/02/2015 Then I have to compare to string from the file (note: I can't change the string) 12/22/2014 or 1/2/2015 and I use:

if l[0] == today:

In second case it obviously failed. My question: how could I change strftime() in order to return only one charachter for month and day when it has preceeding zero?

like image 285
susja Avatar asked Jan 03 '15 17:01

susja


People also ask

What does date () do in Python?

The date() instance method of the python datetime class returns a date instance. Using this method only the date information excluding the time information is retrieved from a datetime instance.


1 Answers

Referring to the documentation, it doesn't appear that there is a character sequence for this. However, you could correct the result as follows:

today = currentDate.strftime('%m/%d/%Y').replace("/0", "/")
if today[0] == '0':
    today = today[1:]

This will eliminate any leading 0s so long as the values are split with a forward slash.

like image 178
OMGtechy Avatar answered Sep 30 '22 15:09

OMGtechy