5 Replies Latest reply on Apr 23, 2010 1:19 AM by comment_1

    Time Formatting Question (in a script)

    Jeff123

      Title

      Time Formatting Question (in a script)

      Post

      In a previous post I asked about a script for Time Zones, but it seems like it would be waaayyy to difficult (or impossible) to do, so I deleted the post in hopes of not cluttering up the forum.  So I elected to use Dialog boxes to display the current time based on a pre-defined time zone field.

       

      Now I am running into some trouble formatting.  I have:

       

       

      MonthName ( Get (CurrentDate ) )  & Day ( Get ( CurrentDate ) ) &  Year ( Get ( CurrentDate) ) & ¶ & 
      
      Hour ( Get (CurrentTime) )  & ":" & Minute ( Get (CurrentTime))

       The month format is exactly what I want.  The problem is with the time format.  I would like it to look like this:

       

      1:32pm but it shows up as 1332 (24 hour clock).

       

      I know there is timestamp, and can get that working, but I don't like the format (The date format and I do not want seconds displayed for the time. I also don't want it to be on the same line).  This, I mind you, is in a Dialog Box, not in a field (I know how to change the time/date format in a field).  

       

      Thanks :smileyhappy:

       

       

        • 1. Re: Time Formatting Question (in a script)
          raybaudi

          Hi

           

          for the time part, try something like:

           

          If ( Hour ( Get ( CurrentTime ) )  ≠  12 ; Mod ( Hour ( Get ( CurrentTime ) ) ; 12 ) ; 12 ) & ":" & Right ( 0 & Minute ( Get ( CurrentTime ) ) ; 2 ) & If ( Hour ( Get ( CurrentTime ) ) > 12 ; "pm" ; "am" )

          • 2. Re: Time Formatting Question (in a script)
            Jeff123

            That helped a whole lot, thank you!

             

            I did run into a small problem, 12:20pm would show up as 12:20am, so I changed your script from

             

            "...Get ( CurrentTime ) ) > 12..." to 

            "...Get ( CurrentTime ) ) > 12..." (the underline isn't showing, it shows as Greater than or equal... you probably did put it in, but it didn't show)

             

            Edited:

             

            Ok, actually I am having more trouble than I thought.  As mentioned above this is meant to show a time a a particular Time Zone.  I am having much trouble with adding hours/minutes, subtracting hours/minutes, and then on top of that, the am/pm.

             

            This is what I wrote (with your help Raybaundi):

             

            For example Add 2 hours to time:

             

            If ( Hour ( Get ( CurrentTime ) )  ≠  12 ; Mod ( Hour ( Get ( CurrentTime ) - Time (2;0;0) ) ; 12 ) ; 12 ) & ":" & Right ( 0 & Minute ( Get ( CurrentTime ) ) ; 2 ) & If ( Hour ( Get ( CurrentTime ) )  ≥  12 ; "pm" ; "am" ) 

             Add 1 hour and 30 minutes to time:

             

            If ( Hour ( Get ( CurrentTime ) )  ≠  12 ; Mod ( Hour ( Get ( CurrentTime ) + Time (1;30;0) ) ; 12 ) ; 12 ) & ":" & Right ( 0 & Minute ( Get ( CurrentTime ) ); 2 ) & If ( Hour ( Get ( CurrentTime ) )  ≥  12 ; "pm" ; "am" ) 

             

            I also tried Hour (1) and Minute (30) instead of Time (1;30;0).  I used it in many different spots and still having trouble.  At one point I pretty much had it but it showed 11pm rather than 11am.  I never came close when I am adding or subtracting minutes.

             

             

             

            The date is working fine, but now I wonder if it is linked so that if it is say 10pm here on April 22, would it say 7am in England on April 23 (for example, I just made up the time difference there).

             

            This is proving to be much more complicated than I originally thought... probably a good thing I scrapped my old idea on how to do it.

             

             

            • 3. Re: Time Formatting Question (in a script)
              comment_1

              I'd suggest you first figure out the time as time, then do the formatting, e.g.

               

              Let (

              t = Get ( CurrentTime ) + Offset
              ;
              Mod ( Hour ( t ) - 1 ; 12 ) + 1
              & SerialIncrement ( ":00" ; Minute ( t ) ) & Case ( t < 43200 ; "am" ; "pm" )
              )

               

              Offset is a Time field where you can input the time difference.

              • 4. Re: Time Formatting Question (in a script)
                Jeff123

                Thank you both.

                 

                I am still having trouble with the "am" and "pm".  If it is PM on my system and the time in another Time Zone is an AM, it still shows as PM.  Maybe a CurrentTimeStamp would work the best?

                 

                So is there a way to format the CurrentTimeStamp in a script.  I figure I can do the Left and Right text functions so that I could break up the date and time on different lines.  However I do not know how I would subsitute the standard "3/04/2010 3:42:54 pm" to simply "Mar 04 03:42 pm".

                 

                It would be important to have the zeros instead of blanks because I think I would have to use the Left and Right text functions to hide a side per line.

                 

                I also figure that if I can do this with CurrentTimeStamp, everything is linked together.  This way if it is 11:00pm on the 23rd here but 01:00am on the 24th there, then it would say Mar 24 01:00am.

                • 5. Re: Time Formatting Question (in a script)
                  comment_1

                   


                  Jeff123 wrote:

                  If it is PM on my system and the time in another Time Zone is an AM, it still shows as PM.


                   

                  That is one reason why I suggested calculating their time before formatting it. This will not happen with the formula I suggested.

                   

                   

                  The same thing applies if you want to include the date:

                   

                  Let (
                  ts = Get ( CurrentTimestamp ) + Offset
                  ;
                  Left ( MonthName ( ts ) ; 3 ) & " " & SerialIncrement ( "00" ; Day ( ts ) )

                  & " " &
                  Mod ( Hour ( ts ) - 1 ; 12 ) + 1 & SerialIncrement ( ":00" ; Minute ( ts ) ) & Case ( GetasTime ( ts ) < 43200 ; "am" ; "pm" )
                  )