日期时间相关操作集合 vjspUtils.date
格式化日期
(static).formatDate(date, format) → {String}
示例
//return "2017-01-12 09:23:07"
vjspUtils.date.formatDate(new Date(),'yyyy-MM-dd hh:mm:ss');
参数:
Name | Type | Description |
---|---|---|
date | Date | 需要格式化的Date |
format | String | 格式字符串 |
返回: 格式化后的字符串
Type String
逆格式化日期字符串
(static).getDateFromFormat(val, format) → {Date}
最好只用来逆格式化由 formatDate 格式化的日期字符串
示例
//return a Date Object value is Thu Jan 12 2017 09:23:07 GMT+0800(中国标准时间)
vjspUtils.date.getDateFromFormat("2017-01-12 09:23:07",'yyyy-MM-dd hh:mm:ss');
参数:
Name | Type | Description |
---|---|---|
val | String | 日期字符串 |
format | String | 格式字符串 |
返回: 格式化后的Date对象
Type Date
时间日期比较
(static).compareDate(date1, date2) → {Integer}
示例
//return -1(date1 < date2)
vjspUtils.date.compareDate('2017-01-12 09:00','2017-01-12 11:00');
参数:
Name | Type | Description |
---|---|---|
date1 | Date|String|Integer | 日期1,Date或者可转为Date的类型 |
date2 | Date|String|Integer | 日期2,Date或者可转为Date的类型 |
返回: 比较结果:date1=date2返回0,date1>date2返回大于0,date1< date2返回小于0
Type Integer
计算date差(date1-date2)
(static) dateDiff(date1, date2, unitopt) → {Integer}
示例
//return 2(hour)
vjspUtils.date.dateDiff('2017-01-12 11:00','2017-01-12 09:00','h');
参数:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
date1 | Date|String|Integer | 日期1,Date或者可转为Date的类型 | ||
date2 | Date|String|Integer | 日期2,Date或者可转为Date的类型 | ||
unit | String | optional | ms | 时间单位(d:天,h:小时,m:分钟,s:秒, ms:毫秒) |
返回: 差值
Type Integer