Page 1 of 1

Date wih weekdays and leap year

Posted: Thu Feb 28, 2019 10:12 am
by socradeath

Code: Select all


init python:
    weekdays = ("mon","tue","wed","thu","fri","sat","sun")
    class Date:
        def __init__(self):
            self.year = 2019
            self.month = 2
            self.day = 28
            self.weekday = weekdays[3]

        def end_day(self):
            self.day += 1
            self.weekday = weekdays[weekdays.index(self.weekday)-6]
            if self.month in (1,3,5,7,8,10,12):
                if self.day > 31:
                    self.day = 1
                    self.month += 1
            elif self.month in (4,6,9,11):
                if self.day > 30:
                    self.day = 1
                    self.month += 1
           
	    #february in leap year
            else :
                # devide by 4
                if (self.year // 4) == 0 :
                    # devide by 100
                    if (self.year // 100) == 0 :
                        # devide by 400
                        if (self.year // 400) == 0 :
                            #leap year
                            __d = 29
                        else:
                            #normal year
                            __d = 28
                    else:
                        #devide by 4 - leap year
                        __d = 29
                else:
                    #normal year
                    __d = 28

                if self.day > __d:
                    self.day = 1
                    self.month += 1
            if self.month > 12:
                self.month = 1
                self.year += 1



        # get weekdays
        def get_weekdays(self):
            if self.month < 3 :
                __m = self.month + 12
                __y = self.year - 1
            else :
                __m = self.month
                __y = self.year
            __weekday = ((__y+__y/4-__y/100+__y/400+(13*__m+8)/5+self.day) % 7) - 1
            self.weekday = weekdays[__weekday]
            return weekdays[__weekday]
        
        def return_date(self):
            return unicode(self.year)+". "+unicode(self.month)+". "+unicode(self.day)





Re: Date wih weekdays

Posted: Thu Feb 28, 2019 10:21 am
by socradeath
I started with monday, but it would be better that start with sunday in many case.

To start with sunday,
weekdays = ["sun","mon",....]

...and...

__weekday = ((__y+__y/4-__y/100+__y/400+(13*__m+8)/5+self.day) % 7)

Re: Date wih weekdays and leap year

Posted: Sat May 02, 2020 4:01 am
by norrissang
Thanks. Is there anyway to show it?

Re: Date wih weekdays and leap year

Posted: Sat Jul 17, 2021 10:03 pm
by rayminator
need more information how to use this