[Solved] How to check if the date is today or note

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller

Post Reply
hylton
Posts: 53
Joined: Thu Oct 04, 2012 12:49 pm

[Solved] How to check if the date is today or note

Post by hylton » Mon Mar 30, 2015 7:01 am

I have the following code which compares today's date with a date I have stored.

Code: Select all

if (item 1 of tTodaysDate - item 1 of tDateIWantToCheck <> 0) or \
(item 2 of tTodaysDate - item 2 of tDateIWantToCheck <> 0) or \
(item 3 of tTodaysDate - item 3 of tDateIWantToCheck <> 0) then
If the result is zero, the stored date is today. If it is not zero, the stored date is not today.

Is there a more elegant use of Livescript to answer the question, Is this date today or not?
Last edited by hylton on Mon Apr 06, 2015 9:11 am, edited 1 time in total.

Lagi Pittas
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 349
Joined: Mon Jun 10, 2013 1:32 pm

Re: How to check if the date is today or not

Post by Lagi Pittas » Mon Mar 30, 2015 10:58 am

Hi

"the date" - will get you today's date
but that will return "03/30/15" for me today (30th of March 2015) the American English Format
so I need the system date which will pass back the date in the User's home country format (i.e. what is setup in the regional settings)

So putting "30/03/2015" into dd

put dd = (the system date) ==> true

The function for "the date" is date() but I don't know if their are any parameters to get it to pass the system date
so :-

put dd = date() ===> false

but ..

set UseSystemDate to true
put dd = date() ===> true

One thing to notice is that "the date" or date() passes back a 4 digit year in the date and the test
seems to be a string not a date test because if dd = "30/03/15" the test fails

Anybody with a better grasp care to help here?

If it's any use here is my library routine that I use for these sorts of things

Code: Select all

-- Function : DatesCompare()
-- Remarks  : if Date 1 is Before Date 2 then return -1 , if same Return 0 else 1
--               : Follows Javascript DateObject return Values
-- Params   : {date} pdDate1, pDate2
-- Returns  : {Integer) - -1, 0 or 1

Function DatesCompare pDate1, Pdate2
   
   set the usesystemdate to true
   
   if pDate1 is not a date or pDate2 is not a date then return "NaN"
   
   convert pDate1 to Dateitems
   convert pDate2 to DateItems

   -- These are inline with the JavasCript DateObject (and why not?) 
   if pDate1 < pDate2 then return -1
   if pDate1 = pDate2 then return 0
   if pDate1 > pDate2 then return 1
end DatesCompare
Regards Lagi

SparkOut
Posts: 2407
Joined: Sun Sep 23, 2007 4:58 pm

Re: How to check if the date is today or note

Post by SparkOut » Mon Mar 30, 2015 11:06 am

Code: Select all

   --if your stored date is in an appropriate format that converts correctly to dateItems then you can leave this out
   --I put it in because I am manually entering a UK date format
   set the useSystemDate to true
   put "30/03/2015" into tStoredDate
   
   --get today's date
   put the date into tCurrentDate
   
   --convert both dates to matching dateItems strings for comparison
   convert tStoredDate to dateItems
   convert tCurrentDate to dateItems
   
   --compare the items 
   if item 1 to 3 of tStoredDate is item 1 to 3 of tCurrentDate then
      answer "Stored date is today"
   else
      answer "Stored date is another day"
   end if

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 5838
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: How to check if the date is today or note

Post by jacque » Mon Mar 30, 2015 4:57 pm

I think I'd just convert both dates to seconds and compare them. Seconds are not dependent on the date format so you don't need to worry about it. This would only work if there's no time attached so that both dates default to midnight.

Edit: you would need to set the useSystemDate to true.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Lagi Pittas
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 349
Joined: Mon Jun 10, 2013 1:32 pm

Re: How to check if the date is today or note

Post by Lagi Pittas » Mon Mar 30, 2015 5:03 pm

Hi jacque

Thanks for edifying me on that. I thought about the seconds but didn't know it used midnight for all , I just assumed it was "Real Seconds"
and didn't want (know how?) to make the adjustment.

Regards Lagi

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 5838
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: How to check if the date is today or note

Post by jacque » Mon Mar 30, 2015 5:33 pm

The seconds as a property alone is the current seconds. But when converting to a date, it will be the number of seconds for that date. If no time is included in the date then it assumes midnight. If there is a time provided to the convert function then the seconds will be for that date at that time.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

hylton
Posts: 53
Joined: Thu Oct 04, 2012 12:49 pm

Re: How to check if the date is today or note

Post by hylton » Tue Mar 31, 2015 2:32 am

Lagi and jacque:
Thank you for your suggestions. I will see if I can make them work.

SparkOut:
if item 1 to 3 of tStoredDate is item 1 to 3 of tCurrentDate then
That is what I was looking for.

How do you even know that exists? Or, how do you find out that LiveCode can even do that?

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7405
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: How to check if the date is today or note

Post by dunbarx » Tue Mar 31, 2015 2:41 am

Sparkout has a lot of experience with LC. He has used dateItems in many ways in the past, and knows the structure of what that keyword can deliver. Imagine this:

Code: Select all

get the date
convert it to dateItems
if item 7 of it = 2 then answer "Ugh. Monday."
Did you know you do not even have to "get" the date?

Craig Newman

SparkOut
Posts: 2407
Joined: Sun Sep 23, 2007 4:58 pm

Re: How to check if the date is today or note

Post by SparkOut » Tue Mar 31, 2015 7:47 am

In answer to the question focused more on knowing how to compare "item 1 to 3 of tData" I am fairly certain that concept was instilled from my very early learning when I began with Rev 2.8 and I am pretty sure it arose from the "Scripting Conferences". It might have been among some of the other materials that were available at the time, and I remember fondly the basic video tutorial to make a "Hello world" stack that really "switched on" Livecode for me (Runtime Revolution as it was back then). It may be a hallucination or there may once have been a video tutorial that mentioned putting data into a range of chunk types (char 7 to -1 or item 4 to 9 of tVar) but certainly my best introduction was via the scripting conferences. I'll bet the text & chunk expressions or the text munging and regex one contains reference to relevant info.

Jacque kindly hosts the (refreshed) scripting conferences here http://www.hyperactivesw.com/revscriptc ... ences.html

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 5838
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: How to check if the date is today or note

Post by jacque » Tue Mar 31, 2015 3:03 pm

certainly my best introduction was via the scripting conferences.
That makes me happy, I'm so glad the conferences did their job.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”