2024-05-06 20:40:05 +00:00
|
|
|
---
|
2024-10-30 17:04:36 +00:00
|
|
|
date: 20200916
|
2024-05-06 20:40:05 +00:00
|
|
|
id: f41437f1-359e-41c9-894b-e5785c29d729
|
|
|
|
title: CommonJS
|
|
|
|
---
|
|
|
|
|
|
|
|
# Syntax
|
|
|
|
|
|
|
|
``` javascript
|
|
|
|
const ordinal = require("ordinal");
|
|
|
|
const {days, months} = require("date-names");
|
|
|
|
|
|
|
|
exports.formatDate = function(date, format) {
|
|
|
|
return format.replace(/YYYY|M(MMM)?|Do?|dddd/g, tag => {
|
|
|
|
if (tag == "YYYY") return date.getFullYear();
|
|
|
|
if (tag == "M") return date.getMonth();
|
|
|
|
if (tag == "MMMM") return months[date.getMonth()];
|
|
|
|
if (tag == "D") return date.getDate();
|
|
|
|
if (tag == "Do") return ordinal(date.getDate());
|
|
|
|
if (tag == "dddd") return days[date.getDay()];
|
|
|
|
});
|
|
|
|
};
|
|
|
|
```
|
|
|
|
|
|
|
|
``` javascript
|
|
|
|
const {formatDate} = require("./format-date");
|
|
|
|
|
|
|
|
console.log(formatDate(new Date(2017, 9, 13),
|
|
|
|
"dddd the Do"));
|
|
|
|
// → Friday the 13th
|
|
|
|
```
|