Page 1 of 1

Format Current DateTime to 2011 02 01 03:04 ?

Posted: Sat May 07, 2011 11:19 pm
by BarrySumpter
Hi all,

I've had a good look around
Can't seem to get the
Format Current DateTime
2011 02 01 03:04

Any help would be greatly appreciated.

Re: Format Current DateTime to 2011 02 01 03:04 ?

Posted: Sat May 07, 2011 11:33 pm
by BarrySumpter
put the seconds into tNow -- 1304807735
convert tNow to dateItems
put item 1 of tNow into tStamp --2011,5,8,8,35,35,1
repeat with x = 2 to 6
put char -2 to -1 of ("00" & item x of tNow) after tStamp -- 20110508083535
end repeat

put char 1 to 4 of tStamp into strMyYear
put char 5 to 6 of tStamp into strMyMonth
put char 7 to 8 of tStamp into strMyDay
put char 9 to 10 of tStamp into strMyHour
put char 11 to 12 of tStamp into strMyMinute

Answer strMyYear && strMyMonth && strMyDay && strMyHour & ":" & strMyMinute --2011 05 08 08:35

that took 12 steps
Anyone have an easier way?
Like format(DateTime, "yyyy mm dd hh:mm")

Re: Format Current DateTime to 2011 02 01 03:04 ?

Posted: Sun May 08, 2011 2:06 am
by Dixie
Barry...

You could put it into a function if you need to use your format a number of times...

Code: Select all

on mouseUp
   put formatDate(the seconds)
end mouseUp

function formatDate theSeconds
   convert theSeconds to dateItems
   put item 1 to 5 of theSeconds into theSeconds
   repeat with count = 2 to 5
      if the number of chars of item count of theSeconds < 2 then
         put 0 & item count of theSeconds into item count of theSeconds
      end if
   end repeat
   put item 4 of theSeconds & ":" & item 5 of theSeconds into item 4 of theSeconds
   delete item 5 of theSeconds
   replace comma with space in theSeconds
   return theSeconds
end formatDate
be well

Dixie

Re: Format Current DateTime to 2011 02 01 03:04 ?

Posted: Sun May 08, 2011 3:13 am
by BarrySumpter
Dixie rescuses again!

Thanks for all that work.
Champion!

I had considered making it a routine
but was hoping the format or formatDate statement was already built into LiveCode.

Re: Format Current DateTime to 2011 02 01 03:04 ?

Posted: Sun May 08, 2011 3:44 am
by dunbarx
How about:

Code: Select all

on mouseUp
   convert the seconds to dateItems
   
   answer item 1 of it && char -2 of ("0" & item 2 of it) & char -1 of ("0" & item 2 of it) &&\
   char -2 of ("0" & item 3 of it) & char -1 of ("0" & item 3 of it) &&\
   char -2 of ("0" & item 4 of it) & char -1 of ("0" & item 4 of it) &&\
   ":" & char -2 of ("0" & item 5 of it) & char -1 of ("0" & item 5 of it)
end mouseUp
4 steps.

Colin?

Re: Format Current DateTime to 2011 02 01 03:04 ?

Posted: Sun May 08, 2011 12:11 pm
by Dixie
dunbarx...
Lovely... :D
be well

Dixie

Re: Format Current DateTime to 2011 02 01 03:04 ?

Posted: Sun May 08, 2011 2:21 pm
by Klaus
Hi all,

me too! :)

Code: Select all

...
  put the seconds into tNow
  convert tNow to dateItems
  answer item 1 of tNow && format("%02d",item 2 of tNow) && format("%02d",item 3 of tNow) && \
           format("%02d",item 4 of tNow) & ":" & format("%02d",item 5 of tNow)
...
:)


Best from sunny germany

Klaus

Re: Format Current DateTime to 2011 02 01 03:04 ?

Posted: Mon May 09, 2011 2:08 am
by BarrySumpter
Decided on this routine
as it's easier to read
and should be super easy to convert to a funtion with multiple formats.

Well for me anyway.

Code: Select all

on mouseUp
   
   put the seconds into tNow
   
   convert tNow to dateItems
   -- dateItems:  2000,2,17,22,13,21,5
   -- dateItems:  Year, Month, Day, Hour, Minute, Second, Day of week
      
   put item 1 of tNow into tStamp
   
   repeat with x = 2 to 5
      
      if x < 5 then
         put " " after tStamp
      else
         put ":" after tStamp
      end if
      
      Put format("%02d",item x of tNow) after tStamp
      
   end repeat
   
   Answer tStamp
      
end mouseUp
[/size]

Re: Format Current DateTime to 2011 02 01 03:04 ?

Posted: Sun Mar 18, 2012 12:54 pm
by BarrySumpter
well that was nice n handy. :mrgreen: