## With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

No credit card required

# 3.4. Printing a Date or Time in a Specified Format

## Problem

You need to print out a date or time formatted in a particular way.

## Solution

Use `date( )` or `strftime( )`:

```print strftime('%c');
print date('m/d/Y');
Tue Jul 30 11:31:08 2002
07/30/2002```

## Discussion

Both `date( )` and `strftime( )` are flexible functions that can produce a formatted time string with a variety of components. The formatting characters for these functions are listed in Table 3-3. The Windows column indicates whether the formatting character is supported by `strftime( )` on Windows systems.

Table 3-3. strftime( ) and date( ) format characters

Type

strftime( )

date( )

Description

Range

Windows

Hour

`%H`

`H`

Hour, numeric, 24-hour clock

00-23

Yes

Hour

`%I`

`h`

Hour, numeric, 12-hour clock

01-12

Yes

Hour

`%k`

Hour, numeric, 24-hour clock, leading zero as space

0-23

No

Hour

`%l`

Hour, numeric, 12-hour clock, leading zero as space

1-12

No

Hour

`%p`

`A`

AM or PM designation for current locale

Yes

Hour

`%P`

`a`

am/pm designation for current locale

No

Hour

`G`

Hour, numeric, 24-hour clock, leading zero trimmed

0-23

No

Hour

`g`

Hour, numeric, 12-hour clock, leading zero trimmed

0-1

No

Minute

`%M`

`I`

Minute, numeric

00-59

Yes

Second

`%S`

`s`

Second, numeric

00-61[a]

Yes

Day

`%d`

`d`

Day of the month, numeric

01-31

Yes

Day

`%e`

Day of the month, numeric, leading zero as space

1-31

No

Day

`%j`

`z`

Day of the year, numeric

001-366 for `strftime( )`

## With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

No credit card required