1- uni-app
// uni-app 十位数 的 时间戳 转换 时间 年月日 时分秒
js_date_time(unixtime) {
var dateTime = new Date(parseInt(unixtime) * 1000) // 补3个0, 十三位的时间戳
var year = dateTime.getFullYear();
var month = dateTime.getMonth() + 1;
if(month > 0 && month <10){
month = "0" + month;
}
var day = dateTime.getDate();
if(day > 0 && day < 10){
day = "0"+day;
}
var hour = dateTime.getHours();
if(hour > 0 && hour < 10){
hour = "0"+hour;
}
var minute = dateTime.getMinutes();
if(minute > 0 && minute < 10){
minute = "0"+minute;
}
var second = dateTime.getSeconds();
if(second > 0 && second < 10){
second = "0"+second;
}
// var timeSpanStr = year + '-' + month + '-' + day ; // 年月日
var timeSpanStr = year + '-' + month + '-' + day+ ' ' +hour +':'+ minute +':'+ second; // 年月日 时分秒
// 把现在的时间 转换为 时间戳
// var now = new Date(); // 此刻的中国标准时间
// var now_new = Date.parse(now.toDateString()); //typescript转换写法 十三位时间戳
return timeSpanStr;
},
// 调用
this.js_date_time(res.data.data[i].create_time)
OR
this.js_date_time('1635131746')
2- javascript
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>时间戳转时间年月日时分秒</title>
</head>
<body>
<script>
// javascript 版本 时间戳 转换 时间
function js_date_time(unixtime) {
var dateTime = new Date(parseInt(unixtime) * 1000)
var year = dateTime.getFullYear();
var month = dateTime.getMonth() + 1;
if (month > 0 && month < 10) {
month = "0" + month;
}
var day = dateTime.getDate();
if (day > 0 && day < 10) {
day = "0" + day;
}
var hour = dateTime.getHours();
if (hour > 0 && hour < 10) {
hour = "0" + hour;
}
var minute = dateTime.getMinutes();
if (minute > 0 && minute < 10) {
minute = "0" + minute;
}
var second = dateTime.getSeconds();
if (second > 0 && second < 10) {
second = "0" + second;
}
// var timeSpanStr = year + '-' + month + '-' + day ;
var timeSpanStr = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;
// 把现在的时间 转换为 时间戳
// var now = new Date(); // 此刻的中国标准时间
// var now_new = Date.parse(now.toDateString()); //typescript转换写法 十三位时间戳
return timeSpanStr;
}
var time = js_date_time('1635131746')
console.log(time)
</script>
</body>
</html>
第二种
秒 转 时分秒
js
getTime(time) {
// 转换为式分秒
let h = parseInt((time / 60 / 60) % 24);
h = h < 10 ? "0" + h : h;
let m = parseInt((time / 60) % 60);
m = m < 10 ? "0" + m : m;
let s = parseInt(time % 60);
s = s < 10 ? "0" + s : s;
// 作为返回值返回
return [h, m, s];
},
调用
const a = this.getTime(12345)
console.log('时间转换为' + `${a[0]}时${a[1]}分${a[2]}秒`)
第三 时间加一天
html
<a-form-model-item label="时间">
<a-range-picker @change="onChange" v-model="searchform.data" />
</a-form-model-item>
js
export default {
data() {
return {
searchform: {
end_time: "",
start_time: "",
data: undefined,
},
}
}
}
methods
// 高级查询 =============================================
onSubmitsearch() {
// this.params = { ...this.searchform };
console.log(this.params);
if (
this.searchform.start_time !== "" ||
this.searchform.start_time !== undefined
) {
this.params.start_time = this.searchform.start_time;
}
if (
this.searchform.end_time !== "" ||
this.searchform.end_time !== undefined
) {
if (this.searchform.end_time) {
let endstring = "";
let endyear = "";
let endmonth = "";
let endday = "";
endstring = this.addDate(this.searchform.end_time, 1);
endyear = endstring.substring(0, 4);
endmonth = endstring.substring(4, 6);
endday = endstring.substring(6, 8);
this.params.end_time = endyear + "-" + endmonth + "-" + endday;
} else {
this.params.end_time = "";
}
} else {
this.params.end_time = "";
}
},
// 时间
onChange(date, dateString) {
this.searchform.start_time = dateString[0];
this.searchform.end_time = dateString[1];
},
// 时间处理 加一天
addDate(date, days) {
var d = new Date(date);
d.setDate(d.getDate() + days);
var month = d.getMonth() + 1;
var day = d.getDate();
if (month < 10) {
month = "0" + month;
}
if (day < 10) {
day = "0" + day;
}
var val = d.getFullYear() + "" + month + "" + day;
return val;
},
// 格式化时间
formateDate(datetime) {
function addDateZero(num) {
return num < 10 ? "0" + num : num;
}
let d = new Date(datetime);
let formatdatetime =
d.getFullYear() +
"-" +
addDateZero(d.getMonth() + 1) +
"-" +
addDateZero(d.getDate()) +
" " +
addDateZero(d.getHours()) +
":" +
addDateZero(d.getMinutes()) +
":" +
addDateZero(d.getSeconds());
return formatdatetime;
},
2. 日期 加一年,减一天
<text>有效期{{starttime}}至{{endtime}}</text>
有效期 2022-12-12至2023-12-11
import getDateTime from '@/utils/index.js';
methods:{
getdata(){
this.starttime = getDateTime.dateTimeStr('y-m-d');
var d = getDateTime.dateTimeStr('y-m-d',1);
var today = new Date(d);
var t = today.getTime()-1000*60*60*24;
this.endtime = getDateTime.getTime(t);
},
}
utils/index.js 内容
/**
* @param {String} str (y-m-d h:i:s) y:年 m:月 d:日 h:时 i:分 s:秒
* @param {Number} num
*/
function dateTimeStr(str,num){
var date = new Date(),
year = date.getFullYear(), //年
month = date.getMonth() + 1, //月
day = date.getDate(), //日
hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours(), //时
minute = date.getMinutes() < 10 ? date.getMinutes() : date.getMinutes(), //分
second = date.getSeconds() < 10 ? date.getSeconds() : date.getSeconds(); //秒
month >= 1 && month <= 9 ? (month = "0" + month) : "";
day >= 0 && day <= 9 ? (day = "0" + day) : "";
hour >= 0 && hour <= 9 ? hour : "";
minute >= 0 && minute <= 9 ? (minute = "0" + minute) : "";
second >= 0 && second <= 9 ? (second = "0" + second) : "";
if(str.indexOf('y') != -1){
str = str.replace('y', year)
}
if(str.indexOf('m') != -1){
str = str.replace('m', month)
}
if(str.indexOf('d') != -1){
str = str.replace('d', day)
}
if(str.indexOf('h') != -1){
str = str.replace('h', hour)
}
if(str.indexOf('i') != -1){
str = str.replace('i', minute)
}
if(str.indexOf('s') != -1){
str = str.replace('s', second)
}
// 加一年
if(num){
str = year + 1 + '-' + month + '-' + day
}
return str;
}