CTIME Formatting Guide

Complete guide to CTIME timestamp formatting with 30+ directives, examples, and real-world use cases for logs, APIs, emails, and databases.

Overview

CTIME formatting provides a way to format timestamps using directives similar to the Unix strftime function. This guide explains how to use CTIME format strings with practical examples.

Basic Concept

CTIME formatting works by replacing format directives (special character sequences starting with %) with their corresponding components from a date/time value. For example:

  • %Y represents the full 4-digit year

  • %m represents the month (zero-padded)

  • %d represents the day of month (zero-padded)

Common Timestamp Format Examples

ISO 8601 Format

Format:  %F %T
Example: 2025-12-23 14:30:45

Equivalent to: %Y-%m-%d %H:%M:%S

US Short Date Format

Format:  %m/%d/%y
Example: 12/23/25

Same as the %D or %x directive

Full Date and Time

Complete date and time representation

12-Hour Time with AM/PM

24-Hour Time Only

Equivalent to: %H:%M

For 24-hour time with seconds:

Equivalent to: %H:%M:%S

RFC 2822 Style

Log Timestamp Format

Verbose Format

Directive Reference

Year

Directive
Description
Example

%Y

Year, zero-padded (0001-9999)

2025

%y

Year, last two digits

25

Month

Directive
Description
Example

%m

Month as decimal (01-12)

12

%o

Month space-padded (1-12)

12

%q

Month unpadded (1-12)

12

%B

Full month name

December

%b, %h

Abbreviated month

Dec

Day of Month

Directive
Description
Example

%d

Day zero-padded (01-31)

23

%e

Day space-padded (1-31)

23

%g

Day unpadded (1-31)

23

Weekday

Directive
Description
Example

%A

Full weekday name

Monday

%a

Abbreviated weekday

Mon

Hour (24-hour clock)

Directive
Description
Example

%H

Hour zero-padded (00-23)

14

Hour (12-hour clock)

Directive
Description
Example

%I

Hour zero-padded (01-12)

02

%l

Hour unpadded (0-12)

2

%p

Locale AM/PM

PM

%P

Locale am/pm

pm

Minute

Directive
Description
Example

%M

Minute zero-padded (00-59)

30

Second

Directive
Description
Example

%S

Second zero-padded (00-59)

45

Fractional Seconds

Directive
Description
Example

%L

Millisecond (000-999)

500

%f

Microsecond (000000-999999)

500000

%s

Nanosecond (000000000-999999999)

500000000

Timezone

Directive
Description
Example

%Z

Timezone name/abbreviation

MST

%z

UTC offset (±HHMM[SS[.ffffff]])

-0700

%w

UTC offset with seconds

-070000

%i

UTC offset (short form)

-07

%j

UTC offset with colons

-07:00

%k

UTC offset full format

-07:00:00

Composite Formats

Directive
Description
Equivalent

%D, %x

Short date (MM/DD/YY)

%m/%d/%y

%F

ISO 8601 date (YYYY-MM-DD)

%Y-%m-%d

%T, %X

ISO 8601 time (HH:MM:SS)

%H:%M:%S

%R

24-hour time (HH:MM)

%H:%M

%r

12-hour time with AM/PM

HH:MM:SS am/pm

%c

Full date and time

Mon Jan 02 15:04:05 2006

Special Characters

Directive
Description

%n

Newline character

%t

Horizontal tab character

%%

Literal % sign

Practical Usage Tips

  1. Combining Directives: You can mix multiple directives in a single format string

  2. Padding Variations: Different directives provide different padding options for the same component

  3. Timezone Information: Include timezone data for complete timestamp information

  4. Readable Formats: Use full month and weekday names for human-readable timestamps

  5. Millisecond Precision: Include %L for application logs

Example Use Cases

Server Log Entry

API Response Timestamp

Email Header Format

Database Audit Log

User-Friendly Display

Last updated

Was this helpful?