Last active
November 24, 2021 15:19
-
-
Save deepakshrma/29acb997ee2e1d40ce436500d33e766a to your computer and use it in GitHub Desktop.
JavaScript Weird Part: Working with Date code samples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* today: Get the current date | |
*/ | |
exports.today = () => new Date(); | |
// main.js | |
const { today } = require("./date"); | |
console.log(today()); // 2021-08-22T15:59:47.482Z |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// date.js | |
/** | |
* now: Get the current timestamp | |
*/ | |
exports.now = () => Date.now(); | |
// main.js | |
const { today, now } = require("./date"); | |
console.log(now()); // 1629647987489 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// date.js | |
const [HR, DAY, WEEK] = [ | |
60 * 60 * 1000, | |
60 * 60 * 1000 * 24, | |
60 * 60 * 1000 * 24 * 7, | |
]; | |
exports.hourBefore = () => new Date(this.now() - HR); | |
exports.dayBefore = () => new Date(this.now() - DAY); | |
exports.weekBefore = () => new Date(this.now() - WEEK); | |
//main.js | |
console.log(hourBefore()); //2021-08-22T14:59:47.489Z | |
console.log(dayBefore()); // 2021-08-21T15:59:47.489Z | |
console.log(weekBefore()); // 2021-08-15T15:59:47.489Z |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//date.js | |
const MONTHS = { 0: "January", 1: "February", 2: "March", 7: "August" }; // TODO: complete | |
exports.monthInString = (date) => { | |
return MONTHS[date.getMonth()]; | |
}; | |
const DAYS = { 0: "Sunday", 1: "Monday", 2: "Tuesday", 3: "Wednesday" }; // TODO: complete | |
exports.dayOfWeek = (date) => { | |
return DAYS[date.getDay()]; | |
}; | |
// main.js | |
console.log(monthInString(today())); // August | |
console.log(dayOfWeek(today())); // Monday |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const [month, day, year, hr, min, sec, meridiem] = new Date( | |
2021, | |
08, | |
22, | |
18, | |
08, | |
08 | |
) | |
.toLocaleString("en-US") | |
.split(/\W+/); | |
////Output: 9/22/2021, 8:08:08 PM | |
console.log(month, day, year, hr, min, sec, meridiem); // 9 22 2021 6 08 08 PM |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const [month, day, year, hr, min, sec] = new Date(2021, 08, 22, 18, 08, 08) | |
.toLocaleString("en-US", { | |
day: "2-digit", | |
month: "long", | |
year: "2-digit", | |
hour: "2-digit", | |
minute: "2-digit", | |
second: "2-digit", | |
hour12: false, | |
}) | |
.split(/\W+/); //// 9/22/2021, 8:08:08 PM | |
console.log(month, day, year, hr, min, sec); | |
// September 22 21 18 08 08 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Simple hacks working with the month(add/subtract): | |
// date.js | |
/** | |
* addMonths: add a month to date | |
* You can pass -(month) to subtract | |
* | |
* @param {Date} date | |
* @param {number} months | |
*/ | |
exports.addMonths = (date, months) => { | |
var d = date.getDate(); | |
date.setMonth(date.getMonth() + +months); | |
if (date.getDate() != d) { | |
date.setDate(0); | |
} | |
return date; | |
}; | |
exports.subMonths = (date, months) => this.addMonths(date, -1 * months); | |
//main.js | |
console.log(today(), addMonths(today(), 2)); //2021-08-22T16:30:11.826Z 2021-10-22T16:30:11.826Z | |
console.log(today(), subMonths(today(), 2)); //2021-08-22T16:34:10.895Z 2021-06-22T16:34:10.895Z |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0)); | |
// US English uses month-day-year order | |
console.log(new Intl.DateTimeFormat("en-US").format(date)); | |
// → "12/19/2012" | |
// British English uses day-month-year order | |
console.log(new Intl.DateTimeFormat("en-GB").format(date)); | |
// → "19/12/2012" | |
// Chinese | |
console.log(new Intl.DateTimeFormat("zh-TW").format(date)); | |
// → "2012/12/20" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//date.js | |
exports.formatString = (format, date) => { | |
const [MM, DD, YYYY, hh, mm, ss, A] = date | |
.toLocaleString("en-US", { | |
day: "2-digit", | |
month: "2-digit", | |
year: "numeric", | |
hour: "2-digit", | |
minute: "2-digit", | |
second: "2-digit", | |
}) | |
.split(/\W+/); | |
const timeMap = { MM, DD, YYYY, hh, mm, ss, A }; | |
return format.replace(/(\w+)/g, (key) => timeMap[key]); | |
}; | |
// main.js | |
var date = new Date(2012, 11, 20, 3, 0, 0); | |
console.log(formatString("DD-MM-YYYY hh:mm:ss A", date)); // 20-12-2012 03:00:00 AM |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const users = [ | |
{ name: "1", dob: "1989/8/26" }, | |
{ name: "2", dob: "1989/8/23" }, | |
{ name: "3", dob: "1989/8/25" }, | |
]; | |
const sortedUsers = users.sort( | |
({ dob: dob1 }, { dob: dob2 }) => | |
new Date(dob1).getTime() - new Date(dob2).getTime() | |
); | |
console.log(sortedUsers); | |
/** | |
[ | |
{ name: '2', dob: '1989/8/23' }, | |
{ name: '3', dob: '1989/8/25' }, | |
{ name: '1', dob: '1989/8/26' } | |
] | |
**/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//date.js | |
exports.formatDuration = (ms) => { | |
ms = Math.abs(ms); | |
const time = { | |
day: Math.floor(ms / 86400000), | |
hour: Math.floor(ms / 3600000) % 24, | |
minute: Math.floor(ms / 60000) % 60, | |
second: Math.floor(ms / 1000) % 60, | |
millisecond: Math.floor(ms) % 1000, | |
}; | |
return Object.entries(time) | |
.filter((val) => val[1] !== 0) | |
.map(([key, val]) => `${val} ${key}${val !== 1 ? "s" : ""}`) | |
.join(", "); | |
}; | |
// main.js | |
console.log(formatDuration(34325055574)); | |
// 397 days, 6 hours, 44 minutes, 15 seconds, 574 milliseconds |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const currentDate = today(); | |
console.log(currentDate.toISOString()); // 2021-08-22T16:38:27.164Z | |
console.log(currentDate.getTime()); // 1629650307164 | |
console.log(currentDate.getDate()); // 23 | |
console.log(currentDate.getDay()); // 1 | |
console.log(currentDate.getMonth()); // 7 | |
console.log(currentDate.getFullYear()); // 2021 | |
console.log(currentDate.getHours()); // 0 | |
console.log(currentDate.getMinutes()); // 38 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var options = { | |
weekday: "long", | |
year: "numeric", | |
month: "long", | |
day: "numeric", | |
}; | |
console.log(new Intl.DateTimeFormat("de-DE", options).format(date)); | |
// → "Donnerstag, 20. Dezember 2012" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//date.js | |
exports.isValid = (date) => date.toString() !== "Invalid Date"; | |
//main.js | |
console.log(isValid(new Date("2021-08-22T16:30:11.826Z"))); // true | |
console.log(isValid(new Date("ff-08-22T16:30:11.826Z"))); // false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//date.js | |
const isAfterDate = (dateA, dateB) => dateA > dateB; | |
const isBeforeDate = (dateA, dateB) => dateA < dateB; | |
//main.js | |
console.log(new Date(2021, 08, 11) > new Date(2021, 08, 10)); | |
console.log(new Date(2021, 08, 09) > new Date(2021, 08, 10)); | |
console.log( | |
new Date(2021, 08, 10).toISOString() === new Date(2021, 08, 10).toISOString() | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment