In Jurassic Park, time management is crucial. Dinosaur feeding schedules, incident timestamps, enclosure maintenance intervals — all of this requires precise date and time handling. JavaScript's built-in
Date object and the modern Intl API are our tools for this.1// Current date and time
2const now = new Date();
3console.log(now); // Thu Jan 15 2024 10:30:45 GMT+0000
4
5// From a timestamp (milliseconds since Jan 1, 1970)
6const epoch = new Date(0); // Jan 1, 1970
7
8// From a string
9const fromString = new Date("2024-01-15T10:30:45");
10const fromDateString = new Date("2024-01-15");
11
12// From year, month (0-based), day, hour, minute, second
13const specific = new Date(2024, 0, 15, 10, 30, 45); // Jan 15, 2024
14
15// Unix timestamp (milliseconds)
16const timestamp = Date.now();
17const fromTimestamp = new Date(timestamp);1const feedingTime = new Date("2024-01-15T14:30:00");
2
3// Date components
4console.log(feedingTime.getFullYear()); // 2024
5console.log(feedingTime.getMonth()); // 0 (January! months are 0-based)
6console.log(feedingTime.getDate()); // 15 (day of month)
7console.log(feedingTime.getDay()); // 1 (Monday; 0=Sun, 1=Mon, ..., 6=Sat)
8
9// Time components
10console.log(feedingTime.getHours()); // 14
11console.log(feedingTime.getMinutes()); // 30
12console.log(feedingTime.getSeconds()); // 0
13console.log(feedingTime.getMilliseconds()); // 0
14
15// Unix timestamp
16console.log(feedingTime.getTime()); // 1705328200000 (milliseconds since epoch)1const schedule = new Date("2024-01-15T08:00:00");
2
3// Change specific components
4schedule.setHours(14); // Change to 14:00
5schedule.setMinutes(30); // Change to 14:30
6schedule.setDate(16); // Move to the 16th
7
8// Useful for adding time
9const nextFeeding = new Date();
10nextFeeding.setHours(nextFeeding.getHours() + 6); // +6 hours1const date = new Date("2024-01-15T14:30:00");
2
3// Built-in formatting methods
4console.log(date.toString()); // "Mon Jan 15 2024 14:30:00 GMT+0000"
5console.log(date.toDateString()); // "Mon Jan 15 2024"
6console.log(date.toTimeString()); // "14:30:00 GMT+0000"
7console.log(date.toISOString()); // "2024-01-15T14:30:00.000Z"
8console.log(date.toLocaleDateString()); // "1/15/2024" (locale-dependent)
9console.log(date.toLocaleTimeString()); // "2:30:00 PM"1const date = new Date("2024-01-15T14:30:00");
2
3// Basic usage
4const formatter = new Intl.DateTimeFormat('en-US', {
5 weekday: 'long',
6 year: 'numeric',
7 month: 'long',
8 day: 'numeric',
9 hour: '2-digit',
10 minute: '2-digit'
11});
12
13console.log(formatter.format(date));
14// "Monday, January 15, 2024 at 02:30 PM"
15
16// Shorthand
17console.log(date.toLocaleDateString('en-US', {
18 weekday: 'long',
19 year: 'numeric',
20 month: 'long',
21 day: 'numeric'
22}));1// Difference between two dates
2const lastFed = new Date("2024-01-15T08:00:00");
3const now = new Date("2024-01-15T14:30:00");
4
5const diffMs = now - lastFed; // Milliseconds
6const diffSeconds = diffMs / 1000; // Seconds
7const diffMinutes = diffMs / 60000; // Minutes
8const diffHours = diffMs / 3600000; // Hours
9const diffDays = diffMs / 86400000; // Days
10
11console.log(`Last fed ${diffHours} hours ago`); // 6.5 hours
12
13// Adding time
14function addHours(date, hours) {
15 return new Date(date.getTime() + hours * 3600000);
16}
17
18function addDays(date, days) {
19 const result = new Date(date);
20 result.setDate(result.getDate() + days);
21 return result;
22}
23
24const nextMaintenance = addDays(new Date(), 7); // 7 days from now1class FeedingScheduler {
2 constructor() {
3 this.schedules = new Map();
4 }
5
6 addSchedule(dinoId, feedingIntervalHours) {
7 this.schedules.set(dinoId, {
8 lastFed: new Date(),
9 interval: feedingIntervalHours,
10 nextFeeding: new Date(Date.now() + feedingIntervalHours * 3600000)
11 });
12 }
13
14 getNextFeeding(dinoId) {
15 const schedule = this.schedules.get(dinoId);
16 if (!schedule) return null;
17 return schedule.nextFeeding;
18 }
19
20 isOverdue(dinoId) {
21 const schedule = this.schedules.get(dinoId);
22 if (!schedule) return false;
23 return new Date() > schedule.nextFeeding;
24 }
25
26 getOverdueDinosaurs() {
27 const overdue = [];
28 const now = new Date();
29
30 for (const [dinoId, schedule] of this.schedules) {
31 if (now > schedule.nextFeeding) {
32 const overdueHours = (now - schedule.nextFeeding) / 3600000;
33 overdue.push({
34 dinoId,
35 overdueBy: `${overdueHours.toFixed(1)} hours`,
36 nextFeeding: schedule.nextFeeding
37 });
38 }
39 }
40
41 return overdue.sort((a, b) =>
42 this.schedules.get(b.dinoId).nextFeeding - this.schedules.get(a.dinoId).nextFeeding
43 );
44 }
45
46 recordFeeding(dinoId) {
47 const schedule = this.schedules.get(dinoId);
48 if (!schedule) return false;
49
50 const now = new Date();
51 schedule.lastFed = now;
52 schedule.nextFeeding = new Date(now.getTime() + schedule.interval * 3600000);
53
54 console.log(`${dinoId} fed at ${now.toLocaleTimeString()}. Next feeding: ${schedule.nextFeeding.toLocaleTimeString()}`);
55 return true;
56 }
57}
58
59// Usage
60const scheduler = new FeedingScheduler();
61scheduler.addSchedule("TREX-001", 8); // T-Rex every 8 hours
62scheduler.addSchedule("RAPTOR-01", 6); // Raptor every 6 hours
63scheduler.addSchedule("BRACHIOSAURUS", 4); // Herbivore every 4 hours
64
65const overdue = scheduler.getOverdueDinosaurs();
66if (overdue.length > 0) {
67 console.log("Overdue feedings:", overdue);
68}1const rtf = new Intl.RelativeTimeFormat('en', { numeric: 'auto' });
2
3// Relative time display
4console.log(rtf.format(-1, 'day')); // "yesterday"
5console.log(rtf.format(-3, 'hour')); // "3 hours ago"
6console.log(rtf.format(2, 'day')); // "in 2 days"
7console.log(rtf.format(1, 'week')); // "next week"
8
9// Utility function
10function timeAgo(date) {
11 const diff = new Date() - new Date(date);
12 const rtf = new Intl.RelativeTimeFormat('en', { numeric: 'auto' });
13
14 if (diff < 60000) return rtf.format(Math.round(-diff / 1000), 'second');
15 if (diff < 3600000) return rtf.format(Math.round(-diff / 60000), 'minute');
16 if (diff < 86400000) return rtf.format(Math.round(-diff / 3600000), 'hour');
17 return rtf.format(Math.round(-diff / 86400000), 'day');
18}
19
20console.log(timeAgo(new Date(Date.now() - 2 * 3600000))); // "2 hours ago"The Date object and Intl API are fundamental tools for time management in JavaScript. In Jurassic Park, correct time handling determines whether dinosaurs are fed on schedule, whether maintenance is performed on time, and whether incidents are logged with accurate timestamps.
Key points: