Page 1 of 2
convert to date
Posted: Fri Aug 08, 2025 12:16 pm
by CAsba
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 ?
Re: convert to date
Posted: Fri Aug 08, 2025 12:31 pm
by Klaus
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
Re: convert to date
Posted: Fri Aug 08, 2025 2:53 pm
by CAsba
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.
Re: convert to date
Posted: Fri Aug 08, 2025 2:58 pm
by CAsba
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
Re: convert to date
Posted: Fri Aug 08, 2025 3:08 pm
by CAsba
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.
Re: convert to date
Posted: Fri Aug 08, 2025 5:55 pm
by dunbarx
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
Re: convert to date
Posted: Fri Aug 08, 2025 11:15 pm
by Klaus
CAsba,
please post what's in tDate BEFORE you try to convert it!
Re: convert to date
Posted: Sat Aug 09, 2025 12:27 pm
by CAsba
Hi Klaus,
Sorry, I don't understand
"please post what's in tDate"
Re: convert to date
Posted: Sat Aug 09, 2025 3:53 pm
by Klaus
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.
Re: convert to date
Posted: Sat Aug 09, 2025 6:04 pm
by Klaus
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...
Re: convert to date
Posted: Sat Aug 09, 2025 8:13 pm
by jacque
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
Re: convert to date
Posted: Sat Aug 09, 2025 8:25 pm
by SparkOut
You may also experiment with the useSystemDate property
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.
Re: convert to date
Posted: Sun Aug 10, 2025 8:27 am
by CAsba
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.
Re: convert to date
Posted: Sun Aug 10, 2025 12:07 pm
by Klaus
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
Re: convert to date
Posted: Sun Aug 10, 2025 7:16 pm
by SparkOut
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.