convert to date

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: FourthWorld, heatherlaine, Klaus, kevinmiller

CAsba
Posts: 435
Joined: Fri Sep 30, 2022 12:11 pm

convert to date

Post by CAsba » Fri Aug 08, 2025 12:16 pm

Hi
I have this code.

Code: Select all

  set the itemdelimiter to "/"   
         put field "startday" & "/" &  field "startmth" &  "/" & field "startyr" into fld "announce8"
         put field "startmth" & "/" &  field "startday" &  "/" & field "startyr" into date1
         put field "startmth" & "/" &  field "startday" &  "/" & field "startyr" into date2
         convert date1 to long date
field "announce8" is "11/5/25".
The problem is that I cannot get that date into seconds, to enable further scripting.
Is it possible to convert date1 or fld "announce8 into seconds ?

Klaus
Posts: 14198
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: convert to date

Post by Klaus » Fri Aug 08, 2025 12:31 pm

Hi CAsba,

since we are using Lc, it is a simple as:

Code: Select all

...
put fld "announce8" into tDate
convert tDate to seconds
## tDate now contains THE SECONDS!
...
is that what you are looking for?

Not sure what the english date has to look like?
MM/DD/YYYY?
In that case you need to:

Code: Select all

...
put field "startmth" & "/" &  field "startday" &  "/" & field "startyr" into tDate
convert tDate to seconds
## tDate now contains THE SECONDS!
...

Best

Klaus

CAsba
Posts: 435
Joined: Fri Sep 30, 2022 12:11 pm

Re: convert to date

Post by CAsba » Fri Aug 08, 2025 2:53 pm

Thanks Klaus,
But it doesn't work. I have

Code: Select all

 set the itemdelimiter to "/"   
         put field "startday" & "/" &  field "startmth" &  "/" & field "startyr" into fld "announce8"
         put field "startmth" & "/" &  field "startday" &  "/" & field "startyr" into date1
         put field "startmth" & "/" &  field "startday" &  "/" & field "startyr" into date2
         put fld "announce8" into tDate
         convert tDate to seconds
         answer tDate & "27"
         
and this gives me 05/11/2025 27
It looks like fld "announce8" is not recognised as a date.

CAsba
Posts: 435
Joined: Fri Sep 30, 2022 12:11 pm

Re: convert to date

Post by CAsba » Fri Aug 08, 2025 2:58 pm

Klaus,
Latest,

Code: Select all

 put field "startmth" & "/" &  field "startday" &  "/" & field "startyr" into tDate
         convert tDate to seconds
         answer tDate & "27"
still gives same answer

CAsba
Posts: 435
Joined: Fri Sep 30, 2022 12:11 pm

Re: convert to date

Post by CAsba » Fri Aug 08, 2025 3:08 pm

Klaus,
Latest, I tried

Code: Select all

 put fld "startmth" into smth
         put fld "startday" into sday
         put fld "startyr" into syr
         
         set the itemdelimiter to "/"   
         put smth & "/" & sday & "/" & syr into tdate
         #put field "startmth" & "/" &  field "startday" &  "/" & field "startyr" into tDate
         convert tDate to seconds
         answer tDate & "27"
But still got the same answer.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10324
Joined: Wed May 06, 2009 2:28 pm

Re: convert to date

Post by dunbarx » Fri Aug 08, 2025 5:55 pm

Casbah.

place a line of code just before the "convert" line,
if yourVariable is a date then answer "DATE!!"
If that passes, great, if it does not, you need to examine why.

Craig

Klaus
Posts: 14198
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: convert to date

Post by Klaus » Fri Aug 08, 2025 11:15 pm

CAsba,

please post what's in tDate BEFORE you try to convert it!

CAsba
Posts: 435
Joined: Fri Sep 30, 2022 12:11 pm

Re: convert to date

Post by CAsba » Sat Aug 09, 2025 12:27 pm

Hi Klaus,
Sorry, I don't understand

"please post what's in tDate"

Klaus
Posts: 14198
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: convert to date

Post by Klaus » Sat Aug 09, 2025 3:53 pm

I mean the content of the variable tDate right after this line:

Code: Select all

...
 put smth & "/" & sday & "/" & syr into tDate
...
Convert WORKS in any case, but we need to supply valid dates, that's why I want to look at that value.

Klaus
Posts: 14198
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: convert to date

Post by Klaus » Sat Aug 09, 2025 6:04 pm

Juist tested with your example date:

Code: Select all

...
put "05/11/2025" into tDate
convert tDate to seconds
put tDate
## Gives me the correct value: 1746914400
...
So there mus be something else going wrong on your side...

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: convert to date

Post by jacque » Sat Aug 09, 2025 8:13 pm

Are there any invisible characters in the field, like carriage returns or spaces that would create an invalid date? To be sure, you could use this:

Code: Select all

put word 1 of fld "startday" into sday
 put word 1 of  fld "startyr" into syr
That would remove any invalid characters.

BTW, if you do not put the date into a variable, the result of a conversion is placed in the "it" variable:

Code: Select all

convert the date to seconds
answer it
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

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

Re: convert to date

Post by SparkOut » Sat Aug 09, 2025 8:25 pm

You may also experiment with the useSystemDate property

Code: Select all

set the useSystemDate to true

Not that your sample code shows a date that would encounter a problem, but on Windows anything before 1970 is not recognised as a date. A disappointingly ignored problem since forever.

CAsba
Posts: 435
Joined: Fri Sep 30, 2022 12:11 pm

Re: convert to date

Post by CAsba » Sun Aug 10, 2025 8:27 am

Thanks all of you for your interest and insights.
Jacque, I used your suggestion and it works ! Thank you so much for that little wheeze.
So now I can move forward.

Klaus
Posts: 14198
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: convert to date

Post by Klaus » Sun Aug 10, 2025 12:07 pm

Could you please tell us what this info (usesystemdate) actually changed for you?
Your examples here did not give any clue that you are NOT using an english date -> 05/11/2025

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

Re: convert to date

Post by SparkOut » Sun Aug 10, 2025 7:16 pm

Heh, I don't think that was the fix, jwack said to put word 1 of each field into the relevant item of the date to be converted (which woild strip any whitespace/extraneous characters).
Which implies that the original contents of the field were not "clean" values within range/devoid of extra characters.
Lesson = check the input data.

Post Reply