| name | time |
| description | Get current time, date, and timezone information without external APIs. Use when you need to display the current time, calculate time differences, or work with dates and times. |
Purpose
This skill provides methods to get the current time, date, and timezone information using built-in system capabilities without requiring external APIs or services.
When to Use
- Displaying current time/date to users
- Calculating time differences
- Working with timezones
- Formatting dates and times
- Scheduling and time-based operations
- Logging timestamps
Getting Current Time
JavaScript (Node.js/Browser)
const now = new Date();
console.log(now);
console.log(now.toString());
console.log(now.toISOString());
console.log(now.toLocaleString());
console.log(now.getFullYear());
console.log(now.getMonth());
console.log(now.getDate());
console.log(now.getHours());
console.log(now.getMinutes());
console.log(now.getSeconds());
console.log(now.getMilliseconds());
console.log(now.getDay());
Python
from datetime import datetime
import pytz
now = datetime.now()
print(now)
print(now.strftime("%Y-%m-%d %H:%M:%S"))
print(now.isoformat())
print(now.year)
print(now.month)
print(now.day)
print(now.hour)
print(now.minute)
print(now.second)
print(now.weekday())
Bash/Shell
date
date +"%Y-%m-%d %H:%M:%S"
date +%s
date -u
Timezones
JavaScript
const offset = new Date().getTimezoneOffset();
console.log(offset);
const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
console.log(timezone);
const options = { timeZone: 'America/New_York' };
const nyTime = new Date().toLocaleString('en-US', options);
console.log(nyTime);
Python
from datetime import datetime
import pytz
local_tz = datetime.now().astimezone().tzinfo
print(local_tz)
ny_tz = pytz.timezone('America/New_York')
ny_time = datetime.now(ny_tz)
print(ny_time.strftime("%Y-%m-%d %H:%M:%S %Z"))
import pytz
print(pytz.all_timezones[:10])
Time Calculations
JavaScript
const now = new Date();
const tomorrow = new Date(now);
tomorrow.setDate(tomorrow.getDate() + 1);
const nextWeek = new Date(now);
nextWeek.setDate(nextWeek.getDate() + 7);
const nextHour = new Date(now);
nextHour.setHours(nextHour.getHours() + 1);
const yesterday = new Date(now);
yesterday.setDate(yesterday.getDate() - 1);
const date1 = new Date('2025-04-30');
const date2 = new Date('2025-05-01');
const diff = date2 - date1;
const diffDays = diff / (1000 * 60 * 60 * 24);
console.log(diffDays);
const timestamp = Math.floor(Date.now() / 1000);
console.log(timestamp);
const fromTimestamp = new Date(timestamp * 1000);
console.log(fromTimestamp);
Python
from datetime import datetime, timedelta
now = datetime.now()
tomorrow = now + timedelta(days=1)
next_week = now + timedelta(weeks=1)
next_hour = now + timedelta(hours=1)
yesterday = now - timedelta(days=1)
date1 = datetime(2025, 4, 30)
date2 = datetime(2025, 5, 1)
diff = date2 - date1
print(diff.days)
timestamp = int(now.timestamp())
print(timestamp)
from_timestamp = datetime.fromtimestamp(timestamp)
print(from_timestamp)
Formatting
JavaScript
const now = new Date();
console.log(now.toDateString());
console.log(now.toTimeString());
console.log(now.toLocaleDateString());
console.log(now.toLocaleTimeString());
const formatter = new Intl.DateTimeFormat('en-US', {
year: 'numeric',
month: 'long',
day: 'numeric',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
timeZoneName: 'short'
});
console.log(formatter.format(now));
const formatted = now.getFullYear() + '-' +
String(now.getMonth() + 1).padStart(2, '0') + '-' +
String(now.getDate()).padStart(2, '0');
console.log(formatted);
Python
from datetime import datetime
now = datetime.now()
print(now.strftime("%Y-%m-%d"))
print(now.strftime("%H:%M:%S"))
print(now.strftime("%A, %B %d, %Y"))
print(now.strftime("%I:%M %p"))
Common Use Cases
Display Current Time
function getCurrentTime() {
const now = new Date();
return now.toLocaleTimeString();
}
Display Current Date
function getCurrentDate() {
const now = new Date();
return now.toLocaleDateString();
}
Calculate Age
function calculateAge(birthDate) {
const birth = new Date(birthDate);
const now = new Date();
let age = now.getFullYear() - birth.getFullYear();
const monthDiff = now.getMonth() - birth.getMonth();
if (monthDiff < 0 || (monthDiff === 0 && now.getDate() < birth.getDate())) {
age--;
}
return age;
}
Check if Date is in Past
function isPast(date) {
const checkDate = new Date(date);
const now = new Date();
return checkDate < now;
}
Get Start of Day
function getStartOfDay(date) {
const d = new Date(date);
d.setHours(0, 0, 0, 0);
return d;
}
Get End of Day
function getEndOfDay(date) {
const d = new Date(date);
d.setHours(23, 59, 59, 999);
return d;
}
Best Practices
- Always use UTC for storage and calculations
- Use local timezones only for display
- Handle timezone conversions explicitly
- Use ISO 8601 format for data exchange
- Be aware of daylight saving time changes
- Use libraries like moment.js or date-fns for complex operations (JavaScript)
- Use pytz or zoneinfo for Python timezone handling
- Test timezone-related code with different timezones
- Consider leap years and other calendar edge cases
Common Gotchas
Month Indexing
JavaScript months are 0-indexed (0 = January, 11 = December):
const date = new Date(2025, 3, 30);
Timezone Offsets
JavaScript getTimezoneOffset() returns minutes with opposite sign:
const offset = new Date().getTimezoneOffset();
Date Parsing
Avoid parsing date strings with Date constructor (browser-dependent):
const date = new Date('2025-04-30');
const date = new Date('2025-04-30T00:00:00');
Leap Years
February 29 only exists in leap years:
const date = new Date(2025, 1, 29);
Scripts
get-time.js
#!/usr/bin/env node
const now = new Date();
console.log('Current Time:');
console.log('─'.repeat(40));
console.log('ISO:', now.toISOString());
console.log('Local:', now.toLocaleString());
console.log('UTC:', now.toUTCString());
console.log('Unix:', Math.floor(now.getTime() / 1000));
console.log('Timezone:', Intl.DateTimeFormat().resolvedOptions().timeZone);
console.log('─'.repeat(40));
get-time.py
from datetime import datetime
import pytz
now = datetime.now()
utc_now = datetime.utcnow()
print('Current Time:')
print('─' * 40)
print('ISO:', now.isoformat())
print('Local:', now.strftime('%Y-%m-%d %H:%M:%S'))
print('UTC:', utc_now.strftime('%Y-%m-%d %H:%M:%S'))
print('Unix:', int(now.timestamp()))
print('Timezone:', datetime.now().astimezone().tzinfo)
print('─' * 40)