Date & time format 'YYYY-MM-DD hh:mm:ss'

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
fanny
Posts: 5
Joined: Mon Apr 26, 2021 2:54 pm

Date & time format 'YYYY-MM-DD hh:mm:ss'

Post by fanny » Tue Jun 22, 2021 3:23 pm

Hi guys.
I want to get the current time in this format : 'YYYY-MM-DD hh:mm:ss',
- I tried the long and short formats but I need this one.
afterwards I need to verify if this date is between two other dates.
- I get the other dates from the database, thats why I think it needs to be in the format, but I am not entirly sure about it.

Happy to get some thoughts about this.

Klaus
Posts: 13820
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Date & time format 'YYYY-MM-DD hh:mm:ss'

Post by Klaus » Tue Jun 22, 2021 3:42 pm

Hi Fanny,

do it yourself! :-)

Create a function for this like this one, it could of course be a lot shorter, but I wanted to show you the exact way to the solution:

Code: Select all

on mouseUp pMouseButton
   answer your_date_time_format()
end mouseUp

Code: Select all

function your_date_time_format
   put the date into tDateShort
   put the long date into tDateLong
   
   ## Now we need to construct your desired date/time string
   ## We take the YEAR from long date:
   put word -1 of tDateLong into tYear
   ## -> 2021
   
   ## Now to the MONTH and DAY -> 6/22/21
   set itemdel to "/"
   put item 2 of tDateShort into tDay
   ## -> 22
   put item 1 of tDateShort into tMonth
   ## -> 6
   
   ## Now we have to make sure we have a leading ZERO for days and month < 10
   if the num of chars of tDay = 1 then
      put 0 before tDay
   end if
   if the num of chars of tMonth = 1 then
      put 0 before tMonth
   end if
   
   ## Now the long time but without the english AM/PM:
   set the twelvehourtime to FALSE
   
   put the long time into tTime
   ## -> 16:37:27
   
   ## OK, done now return the complete data:
   return tYear & "-" & tMonth & "-" & tDay && tTime
end your_date_time_format
Best

Klaus

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9647
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Date & time format 'YYYY-MM-DD hh:mm:ss'

Post by dunbarx » Tue Jun 22, 2021 4:56 pm

Hi.
afterwards I need to verify if this date is between two other dates.
You need to compare the "seconds" from both dates to see if they are before or after your target date:

Code: Select all

on mouseUp
   put "12/7/98" into firstDate
   put "7/6/1999" into secondDate
   put "1/5/1999" into targetDate
   answer isDateBetween(firstDate,secondDate,targetDate)
end mouseUp

function isDateBetween firstDate secondDate targetDate
   convert firstDate to seconds
   convert secondDate to seconds
   convert targetDate to seconds
   
   return targetDate > firstDate and targetDate < secondDate
end isDateBetween
Know that earlier dates than the beginning of 1970 can also be used. But if you step through the above function, you will see that the seconds return negative values. Not a problem unless your dates are earlier than medieval times. See this silly thread:

viewtopic.php?f=9&t=26619&p=138530&hili ... ng#p138530


Craig

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”