src下边新建文件夹     例:utils

再utils文件新建JS     例:dateFormat.js

内容:

/**
* @part {[num]} 需要返回的天数 0 当前 -1昨天
* @return {[date]} yyyy-mm-dd
*/
export const dateChange = part => {
let today = new Date();
let targetday_milliseconds=today.getTime() + 1000*60*60*24*part;
today.setTime(targetday_milliseconds);
let tYear = today.getFullYear();
let tMonth = today.getMonth();
let tDate = today.getDate();
tMonth = doHandleMonth(tMonth + 1);
tDate = doHandleMonth(tDate);
return tYear+"-"+tMonth+"-"+tDate;

function doHandleMonth(month){
let m = month;
if(month.toString().length == 1){
  m = "0" + month;
};
return m;
}
}


/**
* 时间戳转YYYY-MM-DD
* @param {[type]}
* @return {[type]}
*/
export const formatUnixtimestamp = unixtimestamp => {
var unixtimestamp = new Date(unixtimestamp*1000);
var year = 1900 + unixtimestamp.getYear();
var month = "0" + (unixtimestamp.getMonth() + 1);
var date = "0" + unixtimestamp.getDate();
var hour = "0" + unixtimestamp.getHours();
var minute = "0" + unixtimestamp.getMinutes();
var second = "0" + unixtimestamp.getSeconds();
return year + "-" + month.substring(month.length-2, month.length) + "-" + date.substring(date.length-2, date.length)
+ " " + hour.substring(hour.length-2, hour.length) + ":"
+ minute.substring(minute.length-2, minute.length) + ":"
+ second.substring(second.length-2, second.length);
}

----------------------------------------------------------------------------------------------------------------------------------------------------------------

页面调用:import {dateChange} from '@/utils/dateFormat.js'

方法调用:this.value1 = [dateChange(0)+' 00:00:00',dateChange(0)+' 23:59:59']

posted on 2019-12-02 17:35  Charonっ  阅读(292)  评论(0编辑  收藏  举报