lodash dateformat用法
lodash的dateformat函数用于将日期格式化为指定的字符串。
用法如下:
```javascript
_.dateformat(date, format)
```
参数解释:
- `date`:要格式化的日期对象或日期字符串。
- `format`:格式化字符串。
示例代码:
lodash有哪些方法```javascript
const _ = require('lodash');
// 格式化当前日期为 'YYYY-MM-DD' 格式的字符串
const currentDate = new Date();
const formattedDate = _.dateformat(currentDate, 'YYYY-MM-DD');
console.log(formattedDate); // 输出当前日期的字符串形式
// 格式化指定的日期字符串为 'YYYY年MM月DD日' 格式的字符串
const dateString = '2022-01-01';
const formattedDateString = _.dateformat(dateString, 'YYYY年MM月DD日');
console.log(formattedDateString); // 输出 '2022年01月01日'
```
格式化字符串中的常用格式化选项如下:
- `YYYY`:四位数的年份
- `YY`:两位数的年份
- `MM`:两位数的月份
- `DD`:两位数的日期
- `HH`:24小时制的小时数
- `hh`:12小时制的小时数
- `mm`:分钟数
- `ss`:秒数
上述示例代码中的两个格式化字符串就分别使用了这些格式化选项来生成日期字符串。