js日期时间格式化

 1 formatDate: function (str, fmt) {
 2             if (!str) return '';
 3             fmt = fmt || "yyyy-MM-dd"; //默认只显示年月日
 4             if (this.getIEVersion() > -1) {
 5                 str = str.replace(/-/g, "/")
 6             }
 7             if (typeof str == 'string' && str.indexOf(':') == -1) {
 8                 str += ' 00:00:00';
 9             }
10             date = new Date(str);
11             var o = {
12                     "M+": date.getMonth() + 1, //
13                     "d+": date.getDate(), //
14                     "h+": date.getHours() % 12 == 0 ? 12 : date.getHours() % 12, //
15                     "H+": date.getHours(), //小时
16                     "m+": date.getMinutes(), //
17                     "s+": date.getSeconds(), //
18                     "q+": Math.floor((date.getMonth() + 3) / 3), //
19                     "S": date.getMilliseconds() //毫秒
20                 },
21                 week = {
22                     "0": "\u65e5",
23                     "1": "\u4e00",
24                     "2": "\u4e8c",
25                     "3": "\u4e09",
26                     "4": "\u56db",
27                     "5": "\u4e94",
28                     "6": "\u516d"
29                 };
30             if (/(y+)/.test(fmt)) {
31                 fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
32             }
33             if (/(E+)/.test(fmt)) {
34                 fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? "\u661f\u671f" : "\u5468") : "") + week[date.getDay() + ""]);
35             }
36             for (var k in o) {
37                 if (new RegExp("(" + k + ")").test(fmt)) {
38                     fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
39                 }
40             }
41             return fmt;
42         },

调用方式:

formatDate(item.messageDateTime,'yyyy-MM-dd')

vue写法:
/**
 * 日期格式化
 */
export function dateFormat (date, format) {
  format = format || 'yyyy-MM-dd hh:mm:ss'
  if (date !== 'Invalid Date') {
    const o = {
      'M+': date.getMonth() + 1, // month
      'd+': date.getDate(), // day
      'h+': date.getHours(), // hour
      'm+': date.getMinutes(), // minute
      's+': date.getSeconds(), // second
      'q+': Math.floor((date.getMonth() + 3) / 3), // quarter
      S: date.getMilliseconds() // millisecond
    }
    if (/(y+)/.test(format)) {
      format = format.replace(RegExp.$1,
        (date.getFullYear() + '').substr(4 - RegExp.$1.length))
    }
    for (const k in o) {
      if (new RegExp('(' + k + ')').test(format)) {
        format = format.replace(RegExp.$1,
          RegExp.$1.length === 1 ? o[k]
            : ('00' + o[k]).substr(('' + o[k]).length))
      }
    }
    return format
  }
  return ''
}

  调用方式:

import { dateFormat } from '@/utils/date'
const startT = dateFormat(item.value[0],'yyyy-MM-dd')
 
dateFormat(new Date(new Date().getFullYear() ,0),'yyyy-MM-dd')//当前年份的第一天
dateFormat(new Date(new Date().getFullYear() + 1, 0, 0),'yyyy-MM-dd')//当前年份的最后一天
 
如图:

 

 

var myDate=new Date('22021/12/30');

console.log(myDate) //Thu Dec 30 22021 00:00:00 GMT+0800 (中国标准时间)

myDate.setDate(myDate.getDate()-1);
console.log(myDate) //Wed Dec 29 22021 00:00:00 GMT+0800 (中国标准时间)

 
posted @ 2020-04-27 17:23  星期7  阅读(907)  评论(0编辑  收藏  举报