Skip to content
Plurafy

Date and Time Handling

Working with dates and times in JavaScript

Handle timezone conversions, format dates correctly, and manage recurring events or scheduling.

The Date object represents a single moment in time, though it has limitations. Modern development often uses libraries, but understanding Date is fundamental.

// Current date/time
const now = new Date();
// Specific date
const date1 = new Date('2024-01-15');
const date2 = new Date('2024-01-15T10:30:00');
const date3 = new Date(2024, 0, 15); // Month is 0-indexed
// Unix timestamp (milliseconds since 1970-01-01)
const date4 = new Date(1705316400000);
const timestamp = Date.now(); // Current timestamp
const date = new Date('2024-01-15T14:30:45');
console.log(date.getFullYear()); // 2024
console.log(date.getMonth()); // 0 (January, 0-indexed)
console.log(date.getDate()); // 15
console.log(date.getDay()); // 1 (Monday, 0 = Sunday)
console.log(date.getHours()); // 14
console.log(date.getMinutes()); // 30
console.log(date.getSeconds()); // 45
console.log(date.getTime()); // Timestamp in ms
// ISO string
const isoString = new Date().toISOString();
console.log(isoString); // '2024-01-15T14:30:45.000Z'
// Locale string
const dateString = new Date().toLocaleDateString('en-US');
console.log(dateString); // '1/15/2024'
// Custom formatting
function formatDate(date) {
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
return `${year}-${month}-${day}`;
}
console.log(formatDate(new Date())); // '2024-01-15'
const date = new Date();
// Add days
const nextWeek = new Date(date.getTime() + 7 * 24 * 60 * 60 * 1000);
// Difference between dates
const start = new Date('2024-01-01');
const end = new Date('2024-01-15');
const diffMs = end - start;
const diffDays = Math.floor(diffMs / (1000 * 60 * 60 * 24));
console.log(diffDays); // 14
// Check if past
function isPast(date) {
return date.getTime() < Date.now();
}
// Check if today
function isToday(date) {
const today = new Date();
return date.toDateString() === today.toDateString();
}
// UTC vs Local
const date = new Date();
console.log(date.toISOString()); // UTC
console.log(date.toLocaleString()); // Local timezone
// Get timezone offset
const offset = new Date().getTimezoneOffset(); // Minutes
console.log(offset / 60); // Hours
// Convert to specific timezone (simplified)
function getTimeInTimezone(date, timeZone) {
return new Intl.DateTimeFormat('en-US', {
timeZone,
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
second: '2-digit'
}).format(date);
}
console.log(getTimeInTimezone(new Date(), 'America/New_York'));
// ❌ Problems with Date
new Date('2024-01-15'); // Parses as UTC but returns local time
new Date(2024, 0, 15); // Month is 0-indexed (confusing!)
// ✅ Best practices
// 1. Use ISO 8601 format for storage/transmission
const iso = new Date().toISOString();
// 2. Use libraries for complex operations
// npm install date-fns
// npm install dayjs
// 3. Always consider timezone
const utcDate = new Date('2024-01-15T10:30:00Z');
const localDate = new Date('2024-01-15T10:30:00');