INTERACT FORUM

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 [2]   Go Down

Author Topic: Using Math with Dates  (Read 3382 times)

marko

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 9165
Re: Using Math with Dates
« Reply #50 on: August 17, 2020, 11:48:25 pm »

and(A,B) = if(A,if(B,1,0),0) ... but I agree, if it ain't broken, don't fix it. However in this case I was trying to understand your expression and I found it so convoluted that I just decided to rewrite it.

Regarding Sunday - actually the starting day for %w is Sunday, not Monday as I said above. So the above expressions are returning Sunday-to-Saturday. To adjust for Monday-to-Sunday we need to tweak the calculation a bit, like so:

This Week, starting on Monday:
Code: [Select]
[=isEqual(left([last played,0],5), Math(left(now(),5) - (formatdate(left(now(),5),%w)+6)%7), 6)]=1
Previous Week, starting on Monday:
Code: [Select]
[=save(left(now(),5), today)/
save(left([last played,0],5), played)/
save(Math([today] - (formatdate([today],%w)+6)%7), currWeekStart)/
save(Math([currWeekStart] - 7), prevWeekStart)/
and(isEqual([played], [prevWeekStart], 6), isEqual([played], [currWeekStart], 3))]=1

Only change is in currWeekStart, see if you can figure out what's it doing :)

These expressions work on any week, including rollover on week 52/53-00. They just deal with the day number (today is 44058), calculate the week start day using %w (currWeekStart), and then check if [prevWeekStart] <= [Last Played] < [CurrWeekStart].

Only change is in currWeekStart, see if you can figure out what's it doing :)

Deary me, this made my head hurt...

get the day of week number, add 6, divide by 7, the remainder is the correct number to subtract back to Monday.

This is a really neat approach. As the weeks roll by, the 'day of week number' rotates around 0 - 6, as 'today' gets ever larger. It should always work.

It wouldn't work for me at first, until I realised that I already have library fields named [Today] and [Played] and these were being used in priority over the variable names. Once I changed the variable names all was good.

Re: week 53 into week zero...

Thursday 2nd January 2020 = 43832

formatdate(43832,%w) = 4
Meaning this will take care of the whole "week zero" question in a much more refined and direct way than my solution. I would never have arrived here on my own. Thank you very much for the lesson :)

-marko

zybex

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 2674
Re: Using Math with Dates
« Reply #51 on: August 18, 2020, 02:00:46 am »

It wouldn't work for me at first, until I realised that I already have library fields named [Today] and [Played]

Maybe we should adopt a naming convention for these variables, like _today, _played, _currStartWeek, etc. This would minimize conflicts when sharing code. Underscore seems like a good choice?
Logged

wer

  • Citizen of the Universe
  • *****
  • Posts: 2640
Re: Using Math with Dates
« Reply #52 on: August 18, 2020, 02:07:04 am »

I have previously proposed and advocated for using v_ as a prefix for variables.

That syntax is used in a lot of example code provided on the forums by Mr C, and it is already recognized and highlighted by my Syntax Highlighter (the NPP lexer addon).

I would suggest also adopting it in Zelda.  So in this case, v_today, etc.
Logged

zybex

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 2674
Re: Using Math with Dates
« Reply #53 on: August 18, 2020, 02:27:44 am »

Cool, I wasn't aware of that (before my time). v_ it is.
Logged

zybex

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 2674
Re: Using Math with Dates
« Reply #54 on: August 18, 2020, 05:35:00 am »

Tip: MC uses the same Date numbering as Excel.
On Excel, if you type a date/time into a cell and then format it as a number, you'll get the same value as Now() or ConvertDate() in MC for that date.
Conversely, if you type 44061 and format the cell as Date you'll get today's date.

This is useful to test/debug date math.
Logged
Pages: 1 [2]   Go Up