Trying to get the day of the week (numeric)

Post your questions and help other users.

Moderator: Martin

Post Reply
clive.pottinger
Posts: 19
Joined: 16 May 2014 13:18

Trying to get the day of the week (numeric)

Post by clive.pottinger » 10 Feb 2018 20:53

Hello,

The help page for date formatting indicates that it is possible to get the "stand-alone day of week (Text/Number)" by using the 'c' symbol. However, no matter what I try, all I can get is the short textual day of the week:

Code: Select all

dow = "{getDate(1999,12,31), dateformat, c}";
gives me "Fri".

What do I need to do to get a returned value of 5 instead of "Fri"?

Many thanks.

pmgnt
Posts: 22
Joined: 15 Jul 2016 00:34

Re: Trying to get the day of the week (numeric)

Post by pmgnt » 11 Feb 2018 03:52

dow = "{getDate(1999,12,31), dateformat, EEEE}"

clive.pottinger
Posts: 19
Joined: 16 May 2014 13:18

Re: Trying to get the day of the week (numeric)

Post by clive.pottinger » 11 Feb 2018 05:10

pmgnt wrote:dow = "{getDate(1999,12,31), dateformat, EEEE}"
Unfortunately, this returns "Friday", not 5.

User avatar
Desmanto
Posts: 1935
Joined: 21 Jul 2017 17:50

Re: Trying to get the day of the week (numeric)

Post by Desmanto » 11 Feb 2018 07:57

I never realize there is pattern character 'c' there. I test the pattern and also get "Fri". Seems we missed something out to get the result as number. Trying multiple 'cccc' also give "Friday".

I used to face the same problem, but I simply solved it using division and modulus.

Code: Select all

dow = (getDate(2018,2,10)/ 86400000 + 5) % 7;
divided by 86400000 miliseconds will give result in days after 1 January 1970. Plus 5 because the starting day at 1 January 1970 is Thursday (index 4 from 0 based on Sunday), but my GMT+0700 deduct 7 hours to be less than 1 January 1970. So Friday, 2 January 1970 will be evaluted to 0 at the modulus 7; it should 5 (from 0 based). That's why I plus 5. Depends on your timezone, you maybe need to +4 instead.
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), Resurrection Remix Official 7.0.2 20190714, Pie 9.0, Rooted.

pmgnt
Posts: 22
Joined: 15 Jul 2016 00:34

Re: Trying to get the day of the week (numeric)

Post by pmgnt » 11 Feb 2018 14:38

clive.pottinger wrote:
pmgnt wrote:dow = "{getDate(1999,12,31), dateformat, EEEE}"
Unfortunately, this returns "Friday", not 5.
Sorry my bad. Didnt read the question correctly.

clive.pottinger
Posts: 19
Joined: 16 May 2014 13:18

Re: Trying to get the day of the week (numeric)

Post by clive.pottinger » 11 Feb 2018 14:55

Desmanto wrote:I never realize there is pattern character 'c' there. ... I used to face the same problem, but I simply solved it using division and modulus.

Code: Select all

dow = (getDate(2018,2,10)/ 86400000 + 5) % 7;
Thanks Desmanto. I knew I could do it arithmatically; I just thought I was using 'c' incorrectly. Good to know it doesn't actually work as indicated.
pmgnt wrote:Sorry my bad. Didnt read the question correctly.
No problem.

User avatar
Martin
Posts: 4345
Joined: 09 Nov 2012 14:23

Re: Trying to get the day of the week (numeric)

Post by Martin » 13 Feb 2018 07:31

Hi,

'c' is a strange one that returned the text presentation on most or even all devices. Seems that Google also removed it from the documentation for some reason.
Google added a new pattern 'u' on Android 7+ to get the numeric day of week. I recommend to use this one if you're on a current version of Android.
I'll update the documentation and mark 'c' as deprecated.

Regards,
Martin

clive.pottinger
Posts: 19
Joined: 16 May 2014 13:18

Re: Trying to get the day of the week (numeric)

Post by clive.pottinger » 23 Apr 2019 21:57

Thanks again. Using 'u' worked.

Post Reply