Page 1 of 2

four digit year

Posted: Sat Feb 27, 2016 7:08 pm
by francof
Hi all,
how is it possible to convert a 2 digit year into a 4 digit (16 > 2016)?
I have today date into a field, if I run the app on win I have 27/02/2016 whereas running in android it returns 27/02/16

thanks
franco

Re: four digit year

Posted: Sat Feb 27, 2016 7:18 pm
by jmburnod
Hi Franco,
You might maybe use the seconds to compare dates
Best regards
Jean-Marc

Re: four digit year

Posted: Sat Feb 27, 2016 8:00 pm
by SparkOut
If you look up "convert" you will see various permutations of what you can identify as standardised date/time elements.
If you convert to seconds, you will definitely be able to compare like for like.
Also check "useSystemDate" to see how the user's local settings may be checked for a like for like comparison.

Re: four digit year

Posted: Sat Feb 27, 2016 8:14 pm
by quailcreek
Here's a function I use in my iOS app. I think I adapted it from one of Jacqueline's stacks. You should be able to pick it apart to get what you need.

Code: Select all

function formattedDate pDate
   if pDate is empty or pDate is not a date then put the short english date into pDate
   
   convert pDate from short english date to dateItems
   put item 3 of pDate into theDay
   put item 2 of pDate into theMonth
   put item 1 of pDate into theYear
   put theMonth & "/" & theDay & "/" & theYear into tFormattedDate
   return tFormattedDate
end formattedDate

Re: four digit year

Posted: Sat Feb 27, 2016 9:32 pm
by francof
thanks all for replies but, probably I have not explained my need correctly. I do not want to compare dates I only want to have the year of a date extended, complete.
16 = 2016
15 = 2015

and useSystemDate do not work on android

franco

Re: four digit year

Posted: Sun Feb 28, 2016 12:35 pm
by francof
in the dictionary I've sow, at the voice dateFormat some "incantations" like %m, %d, %y and this:
%Y Four-digit year: This incantation indicates the year as a four-digit number (including the century)

my question is: is it possible to use these incantations to format a date?

ciao
franco

Re: four digit year

Posted: Sun Feb 28, 2016 10:36 pm
by jacque
The dateformat function is read-only. It reports the user's system date format.

The function quailcreek posted is what many of us use. I'm not sure I'm the author of that exact handler but I use one very similar. Parsing the dateitems is the easiest way to do it. You could also try the "format" function if you are comfortable with that.

Re: four digit year

Posted: Mon Feb 29, 2016 12:31 pm
by francof
ok, finally I got the format date that I wanted, dd/mm/yyyy. maybe other shorter and smartest way but, this is what I was able to do:

Code: Select all

 if the environment is "mobile" then                                   --se l'ambiente è mobile elabora la data in formato gg/mm/aa
      put the system date into tdate
      
      set the itemDelimiter to "/"
      if the number of chars OF item 1 of tdate = 1 then
         put 0 BEFORE item 1 of tdate
      end if
      if the number of chars OF item 2 of tdate = 1 then
         put 0 BEFORE item 2 of tdate
      end if
      
      put item 2 of tdate into tGiorno
      put item 1 of tdate into tMese
      
      put the internet date into tDataInternet                                    --ricava l'anno a 4 cifre
      convert tDataInternet from internet date to dateItems
      set itemdel to ","
      put item 1 of tDataInternet into tAnno
      
      put tGiorno & "/" & tMese & "/" & tAnno into field "fldData"
   else
      put the system date into field "fldData"                     --se l'ambiente è win imposta la data del giorno nel campo data
   end if  
ciao
franco

Re: four digit year

Posted: Mon Feb 29, 2016 8:35 pm
by jacque
I see, you want all the date items expanded, not just the year. Your way works fine, you can feel good about that. Here's another way if you're interested:

Code: Select all

function fDate pDate
  if pDate = "" then put the date into pDate
  convert pDate to dateitems
  set the numberformat to "00"
  repeat for each item i in "3,2,1"
    put (item i of pDate + 0) & slash after tFDate -- any math operation applies the numberformat
  end repeat
  delete last char of tFDate -- trailing slash
  return tFDate
end fDate

Re: four digit year

Posted: Tue Mar 01, 2016 6:27 pm
by francof
jacque wrote:I see, you want all the date items expanded, not just the year...
yes, sorry. I don't well explained.
jacque wrote:.. Your way works fine, you can feel good about that...
"I feeeeel good, I knew that I would, now..."
I only have adapted two different codes found here on the forum, sorry I don't remember who are the authors ( for the first part maybe Klaus )

I've tried your solution so:

Code: Select all

on mouseUp
   ask "Data:"
   put it into pDate
   
   put fDate( pDate) into tData
   
   answer tData
end mouseUp

function fDate pDate
   if pDate = "" then put the date into pDate
   convert pDate to dateitems
   set the numberformat to "00"
   repeat for each item i in "3,2,1"
      put (item i of pDate + 0) & slash after tFDate -- any math operation applies the numberformat
   end repeat
   delete last char of tFDate -- trailing slash
   return tFDate
end fDate
very good, but it works only with the date format m/g/yy.
if I'm not wrong, convert command not considers the date format g/m/yy a valid date.
so, if I input manually a date using my natural format, the day first, ( pDate is not empty) pDate is not converted in dateitems.
if pDate is empty and then septate with "the date" the function works correctly.

a question about dateitems: ie. today date: 2016,3,1,0,0,0,3 what are items after the date (0,0,0,3)

ciao
franco

Re: four digit year

Posted: Tue Mar 01, 2016 6:53 pm
by Klaus
Hi Franco,
francof wrote:a question about dateitems: ie. today date: 2016,3,1,0,0,0,3 what are items after the date (0,0,0,3)
the dictionary will not lie to you! 8)


Best

Klaus

Re: four digit year

Posted: Tue Mar 01, 2016 6:59 pm
by quailcreek
Who says Germans don't have a sense of humor. :)

Re: four digit year

Posted: Tue Mar 01, 2016 7:33 pm
by francof
Klaus wrote:Hi Franco,
francof wrote:a question about dateitems: ie. today date: 2016,3,1,0,0,0,3 what are items after the date (0,0,0,3)
the dictionary will not lie to you! 8)


Best

Klaus

hi Klaus,
whoops :oops: I've not thought at it.
so converting "the time" (7:25pm) also returns the date (01/03/16 7:25pm) which items are 2016,3,1,19,25,0,3 (the last for tuesday)

quailcreek wrote:Who says Germans don't have a sense of humor. :)
:roll:

thanks all
franco

Re: four digit year

Posted: Tue Mar 01, 2016 7:45 pm
by Klaus
quailcreek wrote:Who says Germans don't have a sense of humor. :)
:D :D :D

Re: four digit year

Posted: Tue Mar 01, 2016 8:50 pm
by jacque
francof wrote:very good, but it works only with the date format m/g/yy.
if I'm not wrong, convert command not considers the date format g/m/yy a valid date.
In that case you need to tell LC to use the system date format:

Code: Select all

function fDate pDate
  set the useSystemDate to true -- add this line
  if pDate = "" then put the date into pDate
  convert pDate to dateitems
  set the numberformat to "00"
  repeat for each item i in "3,2,1"
    put (item i of pDate + 0) & slash after tFDate -- any math operation applies the numberformat
  end repeat
  delete last char of tFDate -- trailing slash
  return tFDate
end fDate