Flutter/Dart Date Set firstDayOfTheweek as Sunday - flutter

By default, Monday is set as the first day of the week. The middle east region, Sunday is considered the first day of the week.
var now = DateTime.now();
print("todays date is $now");
print("week day is: ${now.weekday}");
// todays date is 2020-09-13 12:20:02.417210
// week day is: 7
since today is Sunday, I want the now.weekday should return as 1.

DateTime.weekday returns an integer in the range 1..7 that corresponds to the DateTime constants of monday, tuesday, etc. The constants are assigned internal values with Monday as the smallest value, but whether you actually treat Monday as the first day of the week is completely up to you.
But if it's easier for you to have weekday values assigned where the value for Sunday is less than the value for Monday, you could just do:
int getAdjustedWeekday(DateTime dateTime) => dateTime.weekday % 7;
And now getAdjustedWeekday will return 0 for Sunday, 1 for Monday, and so on.
If you need values starting from 1 instead of 0, then just add 1:
int getAdjustedWeekday(DateTime dateTime) => 1 + dateTime.weekday % 7;
Note that you would not be able to use the existing DateTime.sunday, DateTime.monday, etc. constants.

You need to convert like below.
var now = DateTime.now();
final now1 = DateFormat("EEEE").format(DateTime.parse(now.toString()));
print("todays date is $now1");
Or if you need int of day then go like below
Declare a day
final day = now1 == "Sunday"
? 1
: now1 == "Monday"
? 2
: now1 == "Tuesday"
? 3
: now1 == "Wednesday"
? 4
: now1 == "Thursday" ? 5 : now1 == "Friday" ? 6 : 7;
And call from widget.
print(day);

Related

Assessing which person is the one with the next birthday

In Stata I am trying to assess which of the given birthdays is the next one compared with a given date. My data looks like this:
All dates are in daily format (%dD_m_Y), e.g. 18mar1926
Variable date which is the reference date with which all other dates should be compared
Variables birth1, birth2, birth3, birth4, birth5, birth6 contain the birthday of all possible household members.
For example: A household with two adults A and B. The birthday of A is 20th Nov 1977 and the birthday of person B is 30th March 1978. The reference date is 29.11.2020. I want to know who is the person who has the next birthday, in the example above it is person B, because person A has had its birthday one week before the reference date, so the next birthday in this household will be celebrated on the 30 March 2021.
Example data:
date
birth1
birth2
birth3
birth4
birth5
birth6
02feb2021
15jan1974
27nov1985
30nov2020
31aug1945
27jun1999
07apr1997
19nov2020
27sep1993
30dec1996
29jan2021
29mar1973
05dec2020
21jan1976
02oct1976
21jan1976
25may1995
15feb1997
25nov2020
25nov1943
29nov1946
02feb2021
28apr1979
EDITED to account for Feb 29
*The edit will treat people who have a February 29 birthday as if it were March 1 in cases when the year of date is not a leap year. If that doesn't make sense for your particular use case, it should be easy to alter the code below as you see fit.
Since you want the next birthday in the year rather than the closest birthday, you can use the year of date and the month and day from birth{i} to create a date for each person's next birthday. Then you can sinmply take the earliest value from each household. I reshape long, and generate a person and household id in order to do this.
Make example data
clear
set obs 6
set seed 1996
generate date = floor((mdy(12,31,2020)-mdy(12,1,2015)+1)*runiform() + mdy(12,1,2015))
format date %td
forvalue i = 1/6 {
gen birth`i' = floor((mdy(12,31,1996)-mdy(12,1,1980)+1)*runiform() + mdy(12,1,1980)) if _n < `i' == 0
format birth`i' %td
}
replace birth6 = birth4 in 6 // want a tie
replace birth2 = date("29feb1996","DMY") in 3 // Feb 29
Find Next Birthday
gen household_id = _n
reshape long birth, i(date household_id) j(person)
drop if mi(birth)
gen person_next_birthday = mdy( month(birth), day(birth), year(date))
* TREAT FEB 29 as if they have a march 1 birthday in non-leap years
replace person_next_birthday = mdy(3,1,year(date)) if month(birth) == 2 ///
& day(birth) == 29 & mod(year(date),4)!=0
replace person_next_birthday = mdy( month(birth), day(birth), year(date) + 1) if person_next_birthday < date
replace person_next_birthday = mdy(3,1,year(date)+1) if month(birth) == 2 ///
& day(birth) == 29 & mod(year(date) + 1,4)!=0 & person_next_birthday < date
format person_next_birthday %td
bysort household_id (person_next_birthday): gen next_bday = person_next_birthday[1]
format next_bday %td
drop person_next_birthday
reshape wide birth, i(date household_id next_bday) j(person)
gen next_bday_persons = ""
* Make a string to present household persons who have next bday
foreach v of varlist birth* {
local person = subinstr("`v'","birth","",.)
local condition = "month(`v') == month(next_bday) & day(`v') == day(next_bday)"
local condition_feb29 = "month(next_bday) == 3 & day(next_bday) == 1 & month(`v') == 2 & day(`v') == 29"
replace next_bday_persons = next_bday_persons + "|`person'" if `condition' | `condition_feb29'
}
replace next_bday_persons = regexr(next_bday_persons,"^\|","")
order next_bday_persons, after(next_bday)
The last loop is unnecessary, but illustrates that this is robust to ties.

Swift Number Of Weeks in Month

I'm using Swift 5. I'm trying to get the number of weeks in a month. Jan 2021 prints 6 weeks, but May prints 5 weeks. I'm thinking the problem is firstWeekday is set to 1. Is there a way to get the number of weeks in a given month without setting firstWeekday?
var localCalendar = Calendar.current
localCalendar.firstWeekday = 1
let weekRange = localCalendar.range(of: .weekOfMonth, in: .month, for: dateFormatter.date(from: monthName)!)
if let weekRange = weekRange {
print("\(monthName) has \(weekRange.count) weeks.")
}
It seems you do not want the number of weeks in a month at all. You want the number of rows in a calendar printout.
The formula for the number of rows needed to represent a month on a standard Gregorian-style calendar is as follows.
Start with the number of days in the month. Add to that the number of blank days before the start of the month, beginning with Sunday. For example, January 2021 is 31 + 5 = 36, because it's 31 days long but 5 days (Sunday thru Thursday) are not part of it before the start. To put it another way: the first day of January 2021 is a Friday; that is day 5 of the week if we call Sunday day 0, so we get 31 + 5.
Now integer-divide by 7. We need at least that number of rows. So (31+5)/7 using integer division is 5. The question is: is that all the rows we need?
To find out, get the remainder of that division. If it is not zero, add another row. So (31+5)%7 is 1, which tells us that one more day needs to be accommodated so we need another row. That makes 6.
Thus:
// `startingOn` pretends that Sunday is day 0
func rowsNeededForMonthWith(numberOfDays n: Int, startingOn i: Int) -> Int {
let (quot,rem) = (n+i).quotientAndRemainder(dividingBy: 7)
return quot + (rem == 0 ? 0 : 1)
}
Here are some quick sanity tests:
// January 2021
rowsNeededForMonthWith(numberOfDays: 31, startingOn: 5) // 6
// But suppose it had 30 days?
rowsNeededForMonthWith(numberOfDays: 30, startingOn: 5) // 5
// Or suppose it had started on Thursday? (cf July 2021)
rowsNeededForMonthWith(numberOfDays: 31, startingOn: 4) // 5

Serial date number since "Jan 1, 0000": is this -1 BC or +1 CE?

I was working on dates in Matlab and Octave, and the "serial date number" format is documented as
A single number equal to the number of days since January 0, 0000 in the proleptic ISO calendar (specifying use of the Gregorian calendar).
In Octave, they document
Return the date/time input as a serial day number, with Jan 1, 0000 defined as day 1.
The gregorian calender does not use a year zero. But Matlab and Octave refer to the year zero. Does this mean that they refer to year -1 BC, as in the astronomical year numbering?
Days before "October 15, 1582" are "wrong by as much as eleven days", according to the octave manual, which is considerably smaller than a complete year. So I'm trying to sort out this ambiguity.
Firstly, note that the MATLAB and Octave definitions are equivalent
[MATLAB] N = "number of days since Jan 0, 0000" ⇔ [OCTAVE] "Jan 1, 0000 is day 1"
Since N = 1 on day 1.
The Wikipedia page on "Year Zero" (which you linked to) offers this:
[...] the year 1 BC is followed by AD 1. However, there is a year zero in astronomical year numbering (where it coincides with the Julian year 1 BC) and in ISO 8601:2004 (where it coincides with the Gregorian year 1 BC), as well as in all Buddhist and Hindu calendars.
MATLAB and Octave appear to have followed the ISO standard, as stated in the datetime docs:
datetime arrays represent points in time using the proleptic ISO calendar
So the year zero, and hence the datenum value of 1 days, coincides with the first day of 1BC.
Per the definitions at the top of this answer
"day 1"
= 1/Jan/0000
= datenum(1)
= datetime( 1, 'ConvertFrom', 'datenum' )
= datetime( 0, 0, 1 )
We can test using datenum (number of days) and datetime (datetime type object)
datenum( 0, 0, 1 ) % = 1, as defined by the docs
datetime( 1, 'ConvertFrom', 'datenum' )
% = 1/Jan/0000 00:00:00
datetime( 1 + 366, 'ConvertFrom', 'datenum' )
% = 1/Jan/0001 00:00:00
% First day of year 1 after 366 days (leap year 0000 + 1 for Jan 1 )

Convert string month to numeric

I have a variable period that contains a month as an abbreviated string (i.e. "JAN", "FEB", "MAR", etc). How do I convert period to a numeral (i.e. 1, 2, 3, etc)?
My solution is:
gen fake_date_s = "2000"+period+"1"
gen fake_date = date(fake_date_s, "YMD")
gen month = month(fake_date)
I don't think it's ugly:
clear
input ///
str3 period
JAN
FEB
DEC
end
list
gen monthnum = month(date("2000" + period + "1", "YMD"))
list
This also works:
gen monthnum = month(date(period, "M"))
as it sets the day and the year in the daily date to 01 and 1960, by default.
I'm sure you can find an alternative that doesn't use date functions, but why not use them?
Another approach is:
local i=1
foreach m in `c(Mons)' {
replace month = "`i'" if month == upper("`m'")
local ++i
}
destring month, replace

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