date_parse

date_parse(string $datetime): array

Leniently parses a date string into a field array (no format needed); failure still returns an array - an odd interface that reports via error_count/errors, contrasted with date_create_from_format's strict-format route.

C C23

C23
#include <stdio.h>
#include <string.h>
#include <time.h>

int main(void) {
    /* strptime parses by an explicit format - strict, no format-free mode */
    struct tm tm = {0};
    const char *rest = strptime("2026-06-21 15:30:45", "%Y-%m-%d %H:%M:%S", &tm);
    char buf[64];
    strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", &tm);
    printf("%s\n", buf);
    /* Month names work only through the matching format */
    struct tm tm2 = {0};
    strptime("June 21, 2026", "%B %d, %Y", &tm2);
    strftime(buf, sizeof(buf), "%Y-%m-%d", &tm2);
    printf("lenient-style month format -> %s\n", buf);
    /* A non-matching string returns NULL instead of filling fields */
    struct tm tm3 = {0};
    const char *fail = strptime("definitely not a date", "%Y-%m-%d %H:%M:%S", &tm3);
    printf("failure: %s\n", fail == NULL ? "NULL" : "matched");
    return 0;
}
Output
2026-06-21 15:30:45
lenient-style month format -> 2026-06-21
failure: NULL

C++ C++26

C++26
#include <cstdio>
#include <ctime>
#include <cstring>

int main() {
    // strptime parses by an explicit format - strict, no format-free mode
    struct tm tm = {};
    const char *rest = strptime("2026-06-21 15:30:45", "%Y-%m-%d %H:%M:%S", &tm);
    char buf[64];
    strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", &tm);
    std::printf("%s\n", buf);
    // Month names work only through the matching format
    struct tm tm2 = {};
    strptime("June 21, 2026", "%B %d, %Y", &tm2);
    strftime(buf, sizeof(buf), "%Y-%m-%d", &tm2);
    std::printf("lenient-style month format -> %s\n", buf);
    // A non-matching string returns NULL instead of filling fields
    struct tm tm3 = {};
    const char *fail = strptime("definitely not a date", "%Y-%m-%d %H:%M:%S", &tm3);
    std::printf("failure: %s\n", fail == nullptr ? "NULL" : "matched");
    return 0;
}
Output
2026-06-21 15:30:45
lenient-style month format -> 2026-06-21
failure: NULL

C# v11.0.100

v11.0.100
// TryParse is genuinely lenient - no format needed at all (rare among languages!)
if (DateTime.TryParse("2026-06-21 15:30:45", System.Globalization.CultureInfo.InvariantCulture,
        System.Globalization.DateTimeStyles.None, out var dt))
    Console.WriteLine(dt.ToString("yyyy-MM-dd HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture));
// Natural month-name forms parse without any pattern
if (DateTime.TryParse("June 21, 2026", System.Globalization.CultureInfo.InvariantCulture,
        System.Globalization.DateTimeStyles.None, out var dt2))
    Console.WriteLine("lenient parse -> " + dt2.ToString("yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture));
// TryParseExact is the strict counterpart (like date_create_from_format)
bool strict = DateTime.TryParseExact("2026-06-21", "yyyy-MM-dd",
    System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out _);
Console.WriteLine("strict TryParseExact: " + strict);
bool bad = DateTime.TryParse("definitely not a date", out _);
Console.WriteLine("failure: " + !bad);
Output
2026-06-21 15:30:45
lenient parse -> 2026-06-21
strict TryParseExact: True
failure: True

Go v1.27.4

v1.27.4
package main

import (
	"fmt"
	"time"
)

func main() {
	// time.Parse needs an explicit layout (Go's reference-time format)
	dt, err := time.Parse("2006-01-02 15:04:05", "2026-06-21 15:30:45")
	if err != nil {
		panic(err)
	}
	fmt.Println(dt.Format("2006-01-02 15:04:05"))
	// Month names work only through the matching layout
	dt2, err := time.Parse("January 2, 2006", "June 21, 2026")
	if err != nil {
		panic(err)
	}
	fmt.Println("lenient-style layout ->", dt2.Format("2006-01-02"))
	// A non-matching string returns an error (strict, not lenient)
	_, err = time.Parse("2006-01-02 15:04:05", "definitely not a date")
	fmt.Println("failure: error =", err != nil)
}
Output
2026-06-21 15:30:45
lenient-style layout -> 2026-06-21
failure: error = true

Java v27.0.1

v27.0.1
import java.time.LocalDateTime;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;

public class Main {
    public static void main(String[] args) {
        // DateTimeFormatter parses by an explicit pattern - strict, not lenient
        DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime dt = LocalDateTime.parse("2026-06-21 15:30:45", f);
        System.out.println(dt.format(f));
        // Month names work only through the matching pattern
        DateTimeFormatter f2 = DateTimeFormatter.ofPattern("MMMM d, yyyy", java.util.Locale.ENGLISH);
        LocalDate d2 = LocalDate.parse("June 21, 2026", f2);
        System.out.println("lenient-style month pattern -> " + d2.format(DateTimeFormatter.ISO_LOCAL_DATE));
        // A non-matching string throws DateTimeParseException
        try {
            LocalDateTime.parse("definitely not a date", f);
            System.out.println("failure: parsed");
        } catch (DateTimeParseException e) {
            System.out.println("failure: DateTimeParseException");
        }
    }
}
Output
2026-06-21 15:30:45
lenient-style month pattern -> 2026-06-21
failure: DateTimeParseException

JavaScript v26.13.1

v26.13.1
// new Date / Date.parse: the only format-free parser here, but the spec
// only guarantees ISO - other forms are implementation-defined fallbacks
const iso = new Date("2026-06-21T15:30:45"); // ISO with time: parsed as LOCAL time
const pad = (n) => String(n).padStart(2, "0");
const fields = `${iso.getFullYear()}-${pad(iso.getMonth() + 1)}-${pad(iso.getDate())} ${pad(iso.getHours())}:${pad(iso.getMinutes())}`;
console.log("ISO date-time fields:", fields);
// Date-only ISO form is UTC by spec - deterministic across engines
console.log("ISO date form:", new Date("2026-06-21").toISOString());
// Lenient natural form parses in V8, but is not spec-guaranteed
console.log("lenient month form valid:", Number.isFinite(Date.parse("June 21, 2026")));
console.log("failure: NaN =", Number.isNaN(Date.parse("definitely not a date")));
Output
ISO date-time fields: 2026-06-21 15:30
ISO date form: 2026-06-21T00:00:00.000Z
lenient month form valid: true
failure: NaN = true

PHP v8.6.0

v8.6.0
// date_parse: lenient parsing of a date STRING into fields (no format needed)
$p = date_parse('2026-06-21 15:30:45');
printf("date %04d-%02d-%02d time %02d:%02d:%02d\n", $p['year'], $p['month'], $p['day'], $p['hour'], $p['minute'], $p['second']);
var_dump($p['fraction']);
var_dump($p['is_localtime']);
// Lenient formats parse without a format string at all
$p2 = date_parse('June 21, 2026');
printf("lenient month=%d day=%d year=%d\n", $p2['month'], $p2['day'], $p2['year']);
// Failure still returns an array - carry error_count/warning_count instead
$p3 = date_parse('definitely not a date');
printf("failure keys: errors=%d warnings=%d\n", $p3['error_count'], $p3['warning_count']);
var_dump($p3['year']);
Output
date 2026-06-21 time 15:30:45
float(0)
bool(false)
lenient month=6 day=21 year=2026
failure keys: errors=4 warnings=1
bool(false)

Python v3.15.2

v3.15.2
from datetime import datetime

# strptime needs an explicit format - there is no format-free parser in stdlib
dt = datetime.strptime("2026-06-21 15:30:45", "%Y-%m-%d %H:%M:%S")
print(dt.strftime("%Y-%m-%d %H:%M:%S"))
# Month names work only through the matching format string
dt2 = datetime.strptime("June 21, 2026", "%B %d, %Y")
print("lenient-style month format ->", dt2.strftime("%Y-%m-%d"))
# A non-matching string raises ValueError immediately (strict, not lenient)
try:
    datetime.strptime("definitely not a date", "%Y-%m-%d %H:%M:%S")
except ValueError as e:
    print("failure: ValueError (strict formats only)")
Output
2026-06-21 15:30:45
lenient-style month format -> 2026-06-21
failure: ValueError (strict formats only)

Rust v1.99.1

v1.99.1
use chrono::{NaiveDate, NaiveDateTime};

fn main() {
    // NaiveDateTime::parse_from_str needs an explicit format string
    let dt = NaiveDateTime::parse_from_str("2026-06-21 15:30:45", "%Y-%m-%d %H:%M:%S").unwrap();
    println!("{}", dt.format("%Y-%m-%d %H:%M:%S"));
    // Month names work only through the matching format
    let d2 = NaiveDate::parse_from_str("June 21, 2026", "%B %d, %Y").unwrap();
    println!("lenient-style format -> {}", d2.format("%Y-%m-%d"));
    // A non-matching string returns an Err (strict, not lenient)
    let fail = NaiveDateTime::parse_from_str("definitely not a date", "%Y-%m-%d %H:%M:%S");
    println!("failure: is_err = {}", fail.is_err());
}
Output
2026-06-21 15:30:45
lenient-style format -> 2026-06-21
failure: is_err = true

TypeScript v26.13.1

v26.13.1
// new Date / Date.parse: the only format-free parser here, but the spec
// only guarantees ISO - other forms are implementation-defined fallbacks
const iso = new Date("2026-06-21T15:30:45"); // ISO with time: parsed as LOCAL time
const pad = (n: number): string => String(n).padStart(2, "0");
const fields = `${iso.getFullYear()}-${pad(iso.getMonth() + 1)}-${pad(iso.getDate())} ${pad(iso.getHours())}:${pad(iso.getMinutes())}`;
console.log("ISO date-time fields:", fields);
// Date-only ISO form is UTC by spec - deterministic across engines
console.log("ISO date form:", new Date("2026-06-21").toISOString());
// Lenient natural form parses in V8, but is not spec-guaranteed
console.log("lenient month form valid:", Number.isFinite(Date.parse("June 21, 2026")));
console.log("failure: NaN =", Number.isNaN(Date.parse("definitely not a date")));
Output
ISO date-time fields: 2026-06-21 15:30
ISO date form: 2026-06-21T00:00:00.000Z
lenient month form valid: true
failure: NaN = true

Language differences

C: strptime (POSIX <time.h>) parses strictly by an explicit format - no format-free mode; a mismatch returns NULL; month names parse via %B.

C++: same strptime as C - no lenient parsing in the standard library.

C#: DateTime.TryParse is the rare NATIVE lenient parser - it recognizes many culture-specific forms without a format (always pass InvariantCulture for determinism); TryParseExact is the strict counterpart (like date_create_from_format).

Go: time.Parse parses strictly by a reference-time layout; no format-free mode (the dateparse package is the usual third-party pick).

Java: DateTimeFormatter is strict; lenient parsing means Joda-Time leftovers or a third-party dateparser (no official option).

JavaScript: new Date/Date.parse is format-free - but the spec only guarantees ISO 8601; other natural-language forms are engine fallbacks (lenient in V8, not cross-engine); ISO with time and no zone parses as LOCAL time, date-only ISO as UTC.

PHP: date_parse uses the same lenient grammar as strtotime (format-free) - it returns a field array; failure ALSO returns an array: error_count/errors/warning_count/warnings report the problem (unlike the exceptions/false of most languages, an odd pattern to check); the strict route is date_create_from_format (the contrast entry); getLastErrors() follows the same convention.

Python: datetime.strptime is strict-format (no lenient parser in stdlib) - lenient parsing means the third-party python-dateutil dateutil.parser.parse (format-free, auto-guessing).

Rust: chrono's parse_from_str is strict-format; no lenient parsing (chrono-english-style crates handle natural-language dates).

TypeScript: same as JavaScript - format-free but the spec only guarantees ISO.