Brian Dunning's FileMaker Custom Functions

FormatDateTime ( dateValue ; timeValue ; format )

Returns the dateValue and/or timeValue formatted as specified in format, using PHP-like coding, and allowing for escaped non-formatted text.

  Average rating: 3.9 (52 votes) Log in to vote

Lewis Lorenz   Lewis Lorenz - Show more from this author
Lorenz Companies
http://www.LorenzCom.com

Share on Facebook Share on Twitter

  Sample input:
FormatDateTime ( 9/21/2005 ; 11:06:53 ; "Y-m-d His \zulu\" )
FormatDateTime ( 10-3-2011 ; "" ; "\today is the \jS\ day\" )
  Sample output:
2005-09-21 110653 zulu
today is the 3rd day

  Function definition: (Copy & paste into FileMaker's Edit Custom Function window)

FormatDateTime ( dateValue ; timeValue ; format )

by Lewis C. Lorenz
9/22/2011: Created
1/1/14: Modified

Returns the dateValue and/or timeValue formatted as specified in format.
If dateValue or timeValue are empty current date or current time are used.
Optionally use the backslash as an escape code to include text not intended to be formatted.
Escaped \ is written \\, all other escaped text must be enclosed within an opening \ and a closing \.
Formatting performed using PHP date() function format codes but with Filemaker conventions.

Passing '?' as the whole value of format returns this help text and the following code explanations.

Day -------
d - 01-31
D - Mon-Sun
j - 1-31
l (lowercase 'L') - Sunday-Saturday
N - 1 (Monday) ... 7 (Sunday). (PHP convention: ISO-8601 representation of day of week.)
S - st, nd, rd or th (English ordinal suffix.)
w - 1 (Sunday) ... 7 (Saturday). (PHP convention : 0 (for Sunday) through 6 (for Saturday).)
z - 1-366. (PHP convention: 0-365.)
Week -----
W - 1-54, week of year Jan. 1. (PHP convention: ISO-8601 week number of year, weeks starting on Monday.)
Month ----
F - January-December
m - 01-12
M - Jan-Dec
n - 1-12
t - 28-31
Year -----
L - Leap year (1/0)
o - year number. ISO-8601 year number.
Y - 4 digit year
y - 2 digit year
Time ----
a - am/pm
A - AM/PM
g - Hour 1-12
G - Hour 0-23
h - Hour 01-12
H - Hour 00-23
i - Min 00-59
s - Sec 00-59
u - Microseconds

NOTICE: This is a recursive function, so, if you change the function name remember to also change it in the function code.

Examples:
FormatDateTime ( 9/21/2005 ; 11:06:53 ; "Y-m-d His \zulu\" ) = 2005-09-21 110653 zulu
FormatDateTime ( 10-3-2011 ; "" ; "\today is the \jS\ day\" ) = today is the 3rd day

 

Comments

David Schellenberg   David Schellenberg
Oct 28, 2020
thank you!
 
 
Aug 26, 2022
I believe this code has a bug... Using the "g" formatter with a time that has a 0 hour value results in a 0 rather than 12 being returned
 
 
Aug 26, 2022
Small fix to the "g" formatter:
Exact ( f; "g" ) ; GetAsText ( If ( Hour ( t ) > 12; Hour ( t ) - 12; If (Hour (t) = 0; 12; Hour ( t ) ) ) ) ; // Fixed 0 hour bug
 
 
Aug 28, 2022
Small fix to the "g" formatter:
Exact ( f; "g" ) ; GetAsText ( If ( Hour ( t ) > 12; Hour ( t ) - 12; If (Hour (t) = 0; 12; Hour ( t ) ) ) ) ; // Fixed 0 hour bug
 
Ethan   Ethan, Tactile Medical
Sep 12, 2022
Small fix to the "h" formatted:
Exact ( f; "h" ) ; Right ( GetAsText ( 100 + Case ( Hour ( t ) > 12 ; Hour ( t ) - 12 ; Hour ( t ) = 0 ; 12 ; Hour ( t ) ) ) ; 2 ) ; // Fixed 0 hour bug
 

Log in to post comments.

 

Note: these functions are not guaranteed or supported by BrianDunning.com. Please contact the individual developer with any questions or problems.

Support this website.

This library has been a free commmunity resource for FileMaker users and developers for 20 years. It receives no funding and has no advertisements. If it has helped you out, I'd really appreciate it if you could contribute whatever you think it's worth: