0

I'm supposed to get the excel date of Dec 1 2011 and what day of the week it is and print it out in this format. The Excel date for Thursday, 2011-Dec-1 is 40878. I've been able to get both, but I don't think my method of getting the day using if statements is the best approach. This is my Original script file, so please forgive the roughness. I've checked and I know my solution are right. My only problem is getting a more efficient way to get the day and any suggestions on how to get the month in my final output. We haven't done the date time module yet,so I can't experiment with that.

here is my code:

Year=2011
Month=12
Day=1



Y2=Year-1900
en=int((14-Month)/12)
Y3=Y2-en
m2=Month+12*


l=1+min(Y3,0)+int(Y3/4)-int(Y3/100)+int((Y3+300)/400)

d1=int(-1.63+(m2-1)*30.6)

import math

d2=math.floor(Day+Y3*365+l+d1) #d2 is the final excel date.


Day_Of_Week=((d2%7)-1) 

print "%s"%(d2)


if Day_Of_Week==0:


print "sun"

if Day_Of_Week ==1:
        print "mon"

if Day_Of_Week==2:
        print"tue"
if Day_Of_Week==3:
        print "wed"
if Day_Of_Week==4 :
        print "thur"
if Day_Of_Week==5:
        print "fri"
if Day_Of_Week==6:
        print "sat"

Any Help will be appreciated :)

Zack Bloom
  • 8,309
  • 2
  • 20
  • 27
Zainatin
  • 13
  • 6

3 Answers3

4

How about:

days = ['sun', 'mon', 'tue', 'wed', 'thur', 'fri', 'sat']

print days[Day_Of_week]

Also take a look at this: How do I read a date in Excel format in Python?

Community
  • 1
  • 1
Zack Bloom
  • 8,309
  • 2
  • 20
  • 27
0

"""I'm supposed to get the excel date of Dec 1 2011""": There is no such thing as the Excel date". There are two" date systems in use by Excel, one where the epoch is in 1900 [the default in Windows Excel] and the other using 1904 [the default in Windows for the Mac.

See the xlrd documentation; there's a section up front about dates, and check out the functions that have xldate in their names.

>>> import xlrd
>>> xlrd.xldate.xldate_from_date_tuple((2011,12, 1), 0) # Windows origin
40878.0
>>> xlrd.xldate.xldate_from_date_tuple((2011,12, 1), 1) # Mac origin
39416.0
John Machin
  • 81,303
  • 11
  • 141
  • 189
0

Thanks for all your help,I was able to do it in a better way ,but couldn't post it up until our assignments had been graded.

This is what i did:

from math import floor
def calcExcelDate(Year, Month,Day):
     Yr_Offset=Year-1900  #Determines year offset from starting point.
     Early_Mnth_Correctn=int((14-Month)/12)
     #Early month correction:makes the year have 14 months so the leap day is added at the end of the year
     DateCorrector=(Yr_Offset)-(Early_Mnth_Correctn) #Corrects Date
     MonthCorrector=Month+12*Early_Mnth_Correctn     #Corrects Month
     Leapyr_Calc=1+min(DateCorrector,0)+int(DateCorrector/4)-int(DateCorrector/100)+int ((DateCorrector+300)/400)
     #calculates no of leap years since starting point
     char=int(floor(-1.63+(MonthCorrector-1)*30.6))
     #determines the number of days preceding the given month in a non leap year.
     Excel_Date=(Day+DateCorrector*365+Leapyr_Calc+char )    
     Days=["Monday","Tuesday","Wednesday","Thursday","Friday","saturday","sunday"]
     Months=["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"]
     Offset=2
     dayNo=(Excel_Date-Offset)%7
     dayOfWk=Days[dayNo]
     return "The excel date of %r %r-%r-%r is %r"%(dayOfWk,Day,Months[Month-1],Year,Excel_Date)
Peter O.
  • 32,158
  • 14
  • 82
  • 96
Zainatin
  • 13
  • 6