Page 1 of 3

Day of the week (dow) at DATE $ / TIME $.

Posted: Thu Apr 29, 2021 11:51 am
by mezjoc
I would have needed to determine the day of the week (e.g. Friday). I ask for your help in solving this problem. Thanks: mezjoc

Re: Day of the week (dow) at DATE $ / TIME $.

Posted: Thu Apr 29, 2021 1:02 pm
by Electroguard
UNIX date started from January 1st 1970, and a quick search said it was a monday
Therefore you can use the Annex Unix date functions to work out how many days have elapsed since the beginning of unix time to any date of interest (eg: the current date).
Dividing the elapsed days by 7 will obviously give the number of elapsed weeks.
More importantly, the remainder of dividing by 7 (modulo) will give the number of days which have elapsed since monday.
So something like (elapsed_unix_days MOD 7) can be used to index a simple 7 element array of day names (eg: 1="Monday" to 7="Sunday"),

Unix Time functions
The following functions utilises the time following the “Unix Time Stamp” format :

DATEUNIX(date$), TIMEUNIX(time$), UNIXDATE$(value [,format]), UNIXTIME$(value)

The “Unix Time Stamp” is a way to track time as a running total of seconds.
This count starts at the Unix Epoch on January 1st, 1970 at UTC.
Therefore, the unix time is merely the number of seconds between a particular date and the Unix Epoch.
In synthesys :
- DATEUNIX("01/01/18") returns the number of seconds between the 01/01/1970 and the 01/01/2018 (1514764800)
- UNIXDATE$("1532773308") returns 28/07/18

Re: Day of the week (dow) at DATE $ / TIME $.

Posted: Thu Apr 29, 2021 2:20 pm
by mezjoc
Dear Electroquard!
Thank you for quick answer. The solution is logical. (I could have figured that out on my own :shock: ). I will inform you of the result.

Re: Day of the week (dow) at DATE $ / TIME $.

Posted: Thu Apr 29, 2021 2:28 pm
by Fernando Perez
Will you be working with dates greater than January 1, 1970?
In that case, you can use this statement, which returns you the name of the day of the week:

Code: [Local Link Removed for Guests]

DIM dayWeek$(6) = "Thursday", "Friday", "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday"
cDate$ = "23/07/30"
 
nDay = (dateUnix(cDate$)/86400) MOD 7
wlog nDay, dayWeek$(nDay)
If not, it can be easily set to deliver the number of the day of the week. Do you work with Sunday = 0 or with Monday = 0?

Re: Day of the week (dow) at DATE $ / TIME $.

Posted: Thu Apr 29, 2021 5:22 pm
by mezjoc
Hello!
This is a possible solution.

Code: [Local Link Removed for Guests]

'/program/lcd_20x4.bas/2021.04.29.
'DS18B20/DAT-->NodeMCU/D4/GPIO2,GND,3V3
'LCD/SDA-->D1mini/D2/GPIO4
'LCD/SCL-->D1mini/D3/GPIO0
'LCD/POWER-->GND,5V
i2c.setup 4,0  'SDA-->GPIO4, SCL-->GPIO0
lcd.init 39,20,4
'DIM dayWeek$(6) = "Thursday", "Friday", "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday"
'!!!in Hungarian
DIM dayweek$(6) = "CSUTORTOK", "PENTEK", "SZOMBAT", "VASARNAP", "HETFO", "KEDD", "SZERDA"
timer0 1000, cycle
wait

cycle:
lcd.print 1,1,date$(2)
lcd.print 1,2,time$
'cDate$="08/04/21"  'DD/MM/YY
'nDay = (dateUnix(cDate$)/86400) MOD 7
nday = (dateUnix(date$(1))/86400) MOD 7
lcd.print 1,3,dayweek$(nday)
temp$=tempr$(2,1)
hom=int(val(temp$)*10)/10
hom$=str$(hom, "%3.1f")
lcd.print 1,4,"Temp=" & hom$ & " Cfok    "
return

Re: Day of the week (dow) at DATE $ / TIME $.

Posted: Thu Apr 29, 2021 9:00 pm
by Fernando Perez
I don't think it works correctly, since it uses date$(1) on line 19. And according to the help of Annex, the 1 is for the American format.
image.png
For Hungary I think it is better to use only date$, without formatting.

Re: Day of the week (dow) at DATE $ / TIME $.

Posted: Thu Apr 29, 2021 10:19 pm
by Fernando Perez
I beg your pardon. I did not know that in Hungary the format is yyyy-mm-dd :roll:

Re: Day of the week (dow) at DATE $ / TIME $.

Posted: Fri Apr 30, 2021 4:37 pm
by mezjoc
No problem. We do everything backwards.
Date = YY / MM / DD.
Time = HH: MM: SS.
Name = 1. surname 2. first name
I'm working with Google Translate! ;)

Re: Day of the week (dow) at DATE $ / TIME $.

Posted: Fri Apr 30, 2021 4:48 pm
by mezjoc
Would you help?
Attachment to Canadian format?
DATE $ (2)
Thanks.

Re: Day of the week (dow) at DATE $ / TIME $.

Posted: Fri Apr 30, 2021 6:44 pm
by nedudgi
[quote=mezjoc post_id=653 time=1619800630 user_id=101]
No problem. We do everything backwards.
Date = YY / MM / DD.
Time = HH: MM: SS.
Name = 1. surname 2. first name
I'm working with Google Translate! ;)
[/quote]
I am not sure, that we are backwards. ISO declared the date format sequence YYYYMMDD, a long-long time ago. This format makes sorting by date more easier for computers. [url]https://blog.ansi.org/2019/01/iso-date- ... dard/#gref[/url]