Format Current DateTime to 2011 02 01 03:04 ?

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

Post Reply
BarrySumpter
Posts: 1201
Joined: Sun Apr 24, 2011 2:17 am

Format Current DateTime to 2011 02 01 03:04 ?

Post by BarrySumpter » Sat May 07, 2011 11:19 pm

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.
All my best,
Barry G. Sumpter

Deving on WinXP sp3-32 bit. LC 5.5 Professional Build 1477
Android/iOS/Server Add Ons. OmegaBundle 2011 value ROCKS!
2 HTC HD2 Latest DorimanX Roms
Might have to reconsider LiveCode iOS Developer Program.

BarrySumpter
Posts: 1201
Joined: Sun Apr 24, 2011 2:17 am

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

Post by BarrySumpter » Sat May 07, 2011 11:33 pm

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")
All my best,
Barry G. Sumpter

Deving on WinXP sp3-32 bit. LC 5.5 Professional Build 1477
Android/iOS/Server Add Ons. OmegaBundle 2011 value ROCKS!
2 HTC HD2 Latest DorimanX Roms
Might have to reconsider LiveCode iOS Developer Program.

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am

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

Post by Dixie » Sun May 08, 2011 2:06 am

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

BarrySumpter
Posts: 1201
Joined: Sun Apr 24, 2011 2:17 am

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

Post by BarrySumpter » Sun May 08, 2011 3:13 am

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.
All my best,
Barry G. Sumpter

Deving on WinXP sp3-32 bit. LC 5.5 Professional Build 1477
Android/iOS/Server Add Ons. OmegaBundle 2011 value ROCKS!
2 HTC HD2 Latest DorimanX Roms
Might have to reconsider LiveCode iOS Developer Program.

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

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

Post by dunbarx » Sun May 08, 2011 3:44 am

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?

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am

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

Post by Dixie » Sun May 08, 2011 12:11 pm

dunbarx...
Lovely... :D
be well

Dixie

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

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

Post by Klaus » Sun May 08, 2011 2:21 pm

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

BarrySumpter
Posts: 1201
Joined: Sun Apr 24, 2011 2:17 am

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

Post by BarrySumpter » Mon May 09, 2011 2:08 am

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]
All my best,
Barry G. Sumpter

Deving on WinXP sp3-32 bit. LC 5.5 Professional Build 1477
Android/iOS/Server Add Ons. OmegaBundle 2011 value ROCKS!
2 HTC HD2 Latest DorimanX Roms
Might have to reconsider LiveCode iOS Developer Program.

BarrySumpter
Posts: 1201
Joined: Sun Apr 24, 2011 2:17 am

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

Post by BarrySumpter » Sun Mar 18, 2012 12:54 pm

well that was nice n handy. :mrgreen:
All my best,
Barry G. Sumpter

Deving on WinXP sp3-32 bit. LC 5.5 Professional Build 1477
Android/iOS/Server Add Ons. OmegaBundle 2011 value ROCKS!
2 HTC HD2 Latest DorimanX Roms
Might have to reconsider LiveCode iOS Developer Program.

Post Reply