Skip to main content

Dates

Date and time manipulation is crucial in most applications. Here's how different languages handle dates, both with built-in features and popular libraries.

Built-in Date Operations​

Current Date and Time​

// Current date/time
const now = new Date();
console.log(now); // 2024-01-15T10:30:00.000Z

// Get components
now.getFullYear(); // 2024
now.getMonth(); // 0 (January, 0-indexed)
now.getDate(); // 15
now.getHours(); // 10
now.getMinutes(); // 30
now.getSeconds(); // 0
now.getDay(); // 1 (Monday, 0=Sunday)

Creating Dates​

// From string
const date1 = new Date("2024-01-15");
const date2 = new Date("2024-01-15T10:30:00");

// From components
const date3 = new Date(2024, 0, 15); // month is 0-indexed
const date4 = new Date(2024, 0, 15, 10, 30, 0);

// From timestamp
const date5 = new Date(1705312200000); // milliseconds

Formatting Dates​

const date = new Date(2024, 0, 15, 10, 30, 0);

// Basic formatting
date.toISOString(); // "2024-01-15T10:30:00.000Z"
date.toLocaleDateString(); // "1/15/2024" (locale-dependent)
date.toLocaleString(); // "1/15/2024, 10:30:00 AM"

// Custom formatting (manual or use Intl)
const formatter = new Intl.DateTimeFormat('en-US', {
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit'
});
formatter.format(date); // "01/15/2024, 10:30 AM"

Date Arithmetic​

const date = new Date(2024, 0, 15);

// Add/subtract days
date.setDate(date.getDate() + 7); // Add 7 days
date.setDate(date.getDate() - 3); // Subtract 3 days

// Add/subtract months
date.setMonth(date.getMonth() + 1); // Add 1 month

// Add/subtract years
date.setFullYear(date.getFullYear() + 1); // Add 1 year

// Difference in milliseconds
const date1 = new Date(2024, 0, 15);
const date2 = new Date(2024, 0, 20);
const diff = date2 - date1; // 432000000 ms (5 days)
// date-fns (modern, tree-shakeable)
import { format, addDays, differenceInDays, parseISO } from 'date-fns';

const date = new Date(2024, 0, 15);

// Formatting
format(date, 'yyyy-MM-dd'); // "2024-01-15"
format(date, 'MMMM d, yyyy'); // "January 15, 2024"

// Arithmetic
addDays(date, 7); // Add 7 days
differenceInDays(date2, date1); // Difference in days

// Parsing
parseISO("2024-01-15"); // Date object

// moment.js (legacy, but still popular)
const moment = require('moment');

moment().format('YYYY-MM-DD'); // "2024-01-15"
moment().add(7, 'days'); // Add 7 days
moment().subtract(3, 'days'); // Subtract 3 days
moment(date2).diff(moment(date1), 'days'); // Difference

Key Takeaways​

  • Standard library maturity — Java's java.time, Python's datetime, Go's time, and C#'s DateTime provide rich built-in APIs for parsing, formatting, arithmetic, and comparison. For example, Go's time.Parse("2006-01-02", "2024-01-15") and Python's datetime.strptime("2024-01-15", "%Y-%m-%d") both parse dates without extra dependencies. C, Zig, and Rust offer only basic timestamps in the standard library; Rust developers typically use the chrono crate for full date support. If you need dates out of the box, choose Java, Python, Go, or C#; for systems work, plan on adding a date library.

  • Formatting conventions — Each language uses different format strings: Go uses 2006-01-02 15:04:05 (reference date as mnemonic); Python uses %Y-%m-%d %H:%M:%S; C# uses yyyy-MM-dd HH:mm:ss; Java uses DateTimeFormatter.ofPattern("yyyy-MM-dd"). Go's approach is memorable because the reference date is literal: month 01, day 02, hour 15, etc. When switching languages, keep a format cheat sheet handy—mixing these up (e.g., %y vs %Y for year) causes subtle bugs.

  • Timezone handling — Java's ZonedDateTime, Python's dateutil.tz, C#'s NodaTime, and Rust's chrono provide robust timezone support. JavaScript's Date is UTC-centric: getHours() returns local time, but storage is UTC, which can confuse developers. C and C++ require manual handling with localtime/gmtime or external libraries. For applications that need to display or store times in multiple timezones, prefer Java, Python, or C# with NodaTime; avoid C/C++ unless you're prepared to integrate a timezone library.

  • Popular libraries — date-fns (JavaScript), Carbon (PHP), chrono (Rust), and NodaTime (C#) extend built-ins with human-readable formatting, natural parsing, and safer timezone handling. For example, Carbon's $date->diffForHumans() returns "2 hours ago." Zig and C have minimal ecosystem support—most projects use std.time or raw Unix timestamps. If you need advanced date features (e.g., "next Tuesday", "end of month"), choose a language with a mature date library.