Passing an object of a class to a method in the class - class

I wrote a simple date class for practice but I can't seem to pass the objects in the class to the methods within the class. The class is supposed to be able to accept test dates, correct them if they are out of range and then print the correct date. Here is the code
class Date:
month = int
day = int
year = int
def setDate(self, month, day, year)
HIGH_MONTH = 12
HIGHEST_DAYS = ['none',31,29,31,30,31,30,31,31,30,31,30,31]
if month > HIGH_MONTH:
month = HIGH_MONTH
elif month < 1:
month = 1
else:
month = month
if day > HIGHEST_DAYS[month]:
day = HIGHEST_DAYS[month]
elif day < 1:
day = 1
else:
day = day
def showDate(self):
print 'Date:',month,'/',day,'/',year
#These are my tests
birthday=Date()
graduation=Date()
birthday.month=6
birthday.day=24
birthday.year=1984
graduation.setDate(5,36,2016)
birthday.showDate()
graduation.showDate()
What I get is a NameError: global name 'month' is not defined

In order for you to use a "global" variable in your class, assign the variables to self.variable_name as such:
class Date:
month = int
day = int
year = int
def setDate(self, month, day, year):
HIGH_MONTH = 12
HIGHEST_DAYS = [None,31,29,31,30,31,30,31,31,30,31,30,31]
if month > HIGH_MONTH:
month = HIGH_MONTH
elif month < 1:
month = 1
else:
month = month
if day > HIGHEST_DAYS[month]:
day = HIGHEST_DAYS[month]
elif day < 1:
day = 1
else:
day = day
self.year = year
self.month = month
self.day = day
def showDate(self):
print 'Date:',self.month,'/',self.day,'/',self.year
>>> birthday=Date()
>>> graduation=Date()
>>> birthday.month=6
>>> birthday.day=24
>>> birthday.year=1984
>>> graduation.setDate(5,36,2016)
>>> birthday.showDate()
Date: 6 / 24 / 1984
>>> graduation.showDate()
Date: 5 / 31 / 2016
>>>

Related

Flutter how to get dates from week number

I am trying to get dates from week number in flutter. But I dont know how to do it. I got the week number and the days of the week. Now I want to get the dates from the days in this week.
For example:
week 2 Monday date,
week 2 Tuesday date,
week 2 Friday date
I have started with this code but I need to solve it in another way.
int weeknumber = 2;
//1=monday, 2=tuesday, 5=friday
List<int> weekdays = [1,2,5];
int year = 2023;
int totaldays = weeknumber * 7;
final extraDuration = Duration(days: totaldays);
final startDate = DateTime(year);
final dates = startDate.add(extraDuration);
print(dates);
Here is the output: 2023-01-15 00:00:00.000
But I want: 2023-01-09, 2023-01-10, 2023-01-13
What can I do to get these dates?
If you want this output
Monday:2023-01-09 00:00:00.000
Tuesday:2023-01-10 00:00:00.000
Friday:2023-01-13 00:00:00.000
you can work on this code
import 'package:intl/intl.dart';
void main() {
int weeknumber = 2;
//1=monday, 2=tuesday, 5=friday
List<String> weekdays = ['Monday','Tuesday','Friday'];
int year = 2023;
int firstDayOfWeek = (weeknumber-1) * 7;
final extraDuration = Duration(days: firstDayOfWeek);
final startDate = DateTime(year);
final dates = startDate.add(extraDuration);
for (var i = 0; i < 7; i++) {
var newDate = dates.add(Duration(days: i));
if(DateFormat('EEEE').format(newDate) == 'Monday')print('Monday:'+newDate.toString());
else if(DateFormat('EEEE').format(newDate) == 'Tuesday')print('Tuesday:'+newDate.toString());
else if(DateFormat('EEEE').format(newDate) == 'Friday')print('Friday:'+newDate.toString());
}
print(dates);
}

display a periodic date in 1 month dart language

How do I display a periodic date in 1 month, for example, I want a date that appears every 2 days, for example starting from Month 04-2020
2020-04-01
2020-04-03
2020-04-06
Try this code below:
var date = DateTime.fromMicrosecondsSinceEpoch(200000); // initial date
for (int i = 1; i<=5; i++) {
var dateCopy = date;
dateCopy = dateCopy.add(Duration(days: i * 2));
print(dateCopy);
}

how can i calculate the difference between two dates from the user input?

This is my code so far:
import datetime
from time import strptime
leapyear = 0
isValid = False
while not isValid:
in_date = input(" Please in put a year in the format dd/mm/yyyy ")
try:
d = strptime(in_date, '%d/%m/%Y')
isValid=True
except:
print ("This is not in the right format")
date = datetime.date.today().strftime("%d/""%m/""%Y")
in_date = in_date.split('/')
date = date.split('/')
in_date = [int(i) for i in in_date]
date = [int(i) for i in date]
date_f = [str(i) for i in date]
date_f = '/'.join(date_f)
in_date_f = [str(i) for i in in_date]
in_date_f = '/'.join(in_date_f)
newdate = []
in_date[0], in_date[2] = in_date[2], in_date[0]
date[0], date[2] = date[2], date[0]
z = "/"
if in_date > date:
newdate.insert((0),(in_date[2] % date[2]))
newdate.insert((1),(in_date[1] % date[1]))
newdate.insert((2),(in_date[0] % date[0]))
print("Current Date:", date_f)
print("you are:", newdate[2],"year(s)",newdate[1],"month(s) and",newdate[0],"days away from:",in_date_f)
else:
print("Please input a date thats higher than todays.")
At the moment i have taken today's date and then taken away that from the user input date which has to be higher than the current date. but this gives the wrong answer because it hasnt taken in the fact of the days in a month the and the months in a year.
how would i go about doing that?
datetime objects are subtractable and comparable, so there is no problem in doing:
userDate = strptime(in_date, '%d/%m/%Y')
if userDate > datetime.datetime.today():
# ...
or
if userDate.date() > datetime.datetime.today().date():
# ....
or to calculate the difference:
diff = userDate - datetime.datetime.today()

How to convert year month and min information to day num

I want to convert the given year, month and min information to day of year info.
For eg lets say
year 2004, month 2, day 2 = 33rd day of year
how can I do it in matlab?
Get the datenum for Jan 1 of that year, and subtract it from the given yy/mm/dd. For example, today's day of the year:
jan1 = datenum(datestr(now,'yy'),'yy')
now - jan1 + 1
Check the above against here.
For a specific date,
>> yy = 2004; mm = 2; dd = 2;
>> doty = datenum(yy,mm,dd) - datenum(yy,1,0)
doty =
33

Get today -2 (skipping weekend)

How can I get the Today -2 days (the last 2 working days from now)? but skipping the weekend?
Example #1: Today is February 25, I want February 21
Example #2: Today is February 26, I want February 24
PS: Date format is DD/MM/YYYY
I have this, but the result is going forward, should I use datediff or what?:
<%
Dim d
d = DateAdd("m", 1, Now)
d = "01/" & Month(d) & "/" & Year(d)
d = DateAdd("d", -1, d)
If Weekday(d) = 7 Then
d = DateAdd("d", -1, d)
ElseIf Weekday(d) = 1 Then
d = DateAdd("d", -2, d)
End If
Response.Write "Day: " & d
%>
To get your desired result you need to subtract 3 days on Saturdays, 4 days on Sundays and Mondays, and 2 days on all other days. This can be achieved with something like this:
today = Now
num = Weekday(today, vbWednesday)
d = today - (2 + num\5 + num\6)
response.write "Two working days back: " & d
The Weekday function returns a numeric value for each weekday. By basing the week on Wednesday you can calculate the additional number of days you need to subtract from the current date with integer divisions:
num\5 returns 1 for Saturday, Sunday and Monday, and 0 otherwise.
num\6 returns 1 for Sunday and Monday, and 0 otherwise.
Thus the term 2 + num\5 + num\6 becomes 3 for Saturdays, 4 for Sundays and Mondays, and 2 for all other days.
This might be overkill for what you need but here are two routines I use in my scripts to add or subtract workdays while considering weekends and holidays.
Function AddWorkingDays(dtStart, intDays)
' Start/Default case...
AddWorkingDays = CDate(dtStart)
' If positive days, step forward, otherwise step backward...
Dim intStep, intCount
If intDays > 0 Then intStep = 1 Else intStep = -1
Do While intCount <> intDays
AddWorkingDays = AddWorkingDays + intStep
If IsValidDate(AddWorkingDays) Then intCount = intCount + intStep
Loop
End Function
Function IsValidDate(d)
Dim intWeekday, intMonth, intDay
intWeekday = Weekday(d)
intMonth = Month(d)
intDay = Day(d)
' Weekend dates are not acceptable...
If intWeekday = vbSaturday Or intWeekday = vbSunday Then Exit Function
' Holidays are also not acceptable...
If intMonth = 01 Then If intDay = 01 Then Exit Function ' New Year's Day
If intMonth = 07 Then If intDay = 04 Then Exit Function ' Independence Day
If intMonth = 12 Then If intDay = 25 Then Exit Function ' Christmas Day
' Memorial Day is the last Monday in May...
If intWeekday = vbMonday Then If intMonth = 05 Then If intDay >= 25 Then Exit Function
' ... (Thanksgiving, others) ...
' All tests passed. Date is a valid workday...
IsValidDate = True
End Function