朱丽叶

导航

根据时间戳格格式化字符转(dddd-mm-dd )

export function getDateTime(value) {
    var b = new Date(value);
    var year = b.getFullYear()+'-';
    var month = (b.getMonth()+1);
    var date = b.getDate();
    if(month<10){
        month = '0'+ (b.getMonth()+1)+'-';
    }else {
        month = (b.getMonth()+1)+'-';
    }
    if(date<10){
        date = '0'+ (b.getDate());
    }
    var str = String(year)+String(month)+String(date)+ ' ';
    return str;
}

// 使用
import {getDateTime} from "@/utils/utils.js"
getDateTime(date);  // 2022-07-20 



// 方式2
function formatDate(date, format= "yyyy-MM-dd") {
  if (!date) return;
  switch (typeof date) {
    case "string":
      date = new Date(date.replace(/-/, "/"));
      break;
    case "number":
      date = new Date(date);
      break;
    }
  if (!date instanceof Date) return;
  var dict = {
    "yyyy": date.getFullYear(),
    "M": date.getMonth() + 1,
    "d": date.getDate(),
    "H": date.getHours(),
    "m": date.getMinutes(),
    "s": date.getSeconds(),
    "MM": ("" + (date.getMonth() + 101)).substr(1),
    "dd": ("" + (date.getDate() + 100)).substr(1),
    "HH": ("" + (date.getHours() + 100)).substr(1),
    "mm": ("" + (date.getMinutes() + 100)).substr(1),
    "ss": ("" + (date.getSeconds() + 100)).substr(1)
  };
  return format.replace(/(yyyy|MM?|dd?|HH?|ss?|mm?)/g, function () {
    return dict[arguments[0]];
  });
}

posted on 2022-08-14 19:40  朱丽叶  阅读(54)  评论(0)    收藏  举报