0

I want to get the value of datetime using python code ex. 20141104 that is example what I want to get then, How can I get the datetime like that.

import calendar
for month in range(1, 13):
    year = 2014
    make_calendar = calendar.monthcalendar(year, month)
    for weekend in make_calendar:
        for day in weekend:
            if (day != 0):
                parameter = str(year) + str(month) + str(day)
                print parameter

-> I try to get value like example but, the result is 201442. I want to 20140402 not 201442.

I'm in need of help.

dazedconfused
  • 1,312
  • 2
  • 19
  • 26
seodaewon
  • 11
  • 3

3 Answers3

2

user1153551 has shown how to do what you want using the calendar module, but you should consider using the datetime module instead, with its powerful strftime method. The calendar module is great when you need to manipulate and/or format calendar at the month or year level, but for lower level manipulation at the level of individual dates, datetime is probably more suitable.

For example:

#! /usr/bin/env python

from datetime import date, timedelta

#A timedelta object of 1 day
oneday = timedelta(days=1)

year = 2014

#A date object of the start of the year
current_day = date(year, 1, 1)

#Print all the days of the given year in YYYYmmdd format
while current_day.year == year:
    print current_day.strftime("%Y%m%d")
    current_day += oneday
PM 2Ring
  • 54,345
  • 6
  • 82
  • 182
  • i agree with you datetime is quite good for archive easiest way. but in existing OP code need to change everything that may be OP agree or not. But your solution is right thanks. – Jaykumar Patel Nov 04 '14 at 11:50
  • @user1153551: The word you want is "achieve ", not "archive". – PM 2Ring Nov 04 '14 at 12:23
  • @user1153551: I agree that we should give answers using the `calendar` module, since the question was asked in reference to that module, which is why I mentioned your answer at the start of mine, and why I made my comment to lessy. However, `calendar` is overkill for this application, and the OP ought to know how to use `time`, `datetime`, and especially `strftime`, so it is appropriate to point out the alternatives, IMHO. Using `calendar` here is a bit like using `re` when a simple `str.replace` is adequate. – PM 2Ring Nov 04 '14 at 12:25
1

You can use following code to get desired output:

     from time import gmtime, strftime,time, sleep
     date = strftime("%Y%m%d")
     print date
lessy
  • 19
  • 3
  • That's fine for printing today's date (although why you are importing gmtime, time and sleep is a mystery), but seodaewon's question is in relation to printing arbitrary dates using the calendar module. – PM 2Ring Nov 04 '14 at 10:03
0

Use '%02d' % month to archive day, month with leading zero for Example,

>>> import datetime
>>> '%02d' % datetime.date.today().month
'11'

Python Code

import calendar
for month in range(1, 13):
    year = 2014
    make_calendar = calendar.monthcalendar(year, month)
    for weekend in make_calendar:
        for day in weekend:
            if (day != 0):
                parameter = '%02d%02d%02d' % (year, month, day)
                print parameter
Jaykumar Patel
  • 26,836
  • 12
  • 74
  • 76
  • While it works fine for dates in the current era, the year portion of your format string is not ideal. It should be '%d%02d%02d' or maybe '%04d%02d%02d', since the calendar module can produce calendars for dates arbitrarily far in the past or future. – PM 2Ring Nov 04 '14 at 09:59