微信小程序js時間戳與日期格式的轉換方法
一、時間戳轉換成日期格式
1、代碼片段
function timestampToTime(value, type = 0){ var time = new Date(value); var year = time.getFullYear(); var month = time.getMonth() + 1; var date = time.getDate(); var hour = time.getHours(); var minute = time.getMinutes(); var second = time.getSeconds(); month = month < 10 ? "0" + month : month; date = date < 10 ? "0" + date : date; hour = hour < 10 ? "0" + hour : hour; minute = minute < 10 ? "0" + minute : minute; second = second < 10 ? "0" + second : second; var arr = [ year + "-" + month + "-" + date, year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second, year + "年" + month + "月" + date, year + "年" + month + "月" + date + " " + hour + ":" + minute + ":" + second, hour + ":" + minute + ":" + second ] return arr[type]; }
使用方法:
timestampToTime(1591841249) //返回2020-06-11
timestampToTime(1591841249,1) //返回 2020-06-11 10:10:10
timestampToTime(1591841249,2) //返回2020年06月11日
2、微信小程序中,時間戳轉換成日期格式的具體步驟
(1)在utils文件夾下創(chuàng)建一個 js文件,在此js文件中 export 上面的代碼片段
(2)在需要引入的頁面的 js文件中引入,需要注意的是對應的方法名稱和此應的文件路徑
import {timestampToTime} from "../../utils/common.js"
(3)在需要引入的頁面的 js文件中使用此方法
success:(res)=>{ console.log(res); //遍歷每一個對象 res.data.data.forEach(item=>{ item.publish_date=timestampToTime(item.publish_date) }) this.setData({ newsARR:res.data.data }) }
二、日期格式轉換成時間戳
var date = new Date("2022-12-04 17:15:53:555"); // 有三種方式獲取 var time1 = date.getTime(); var time2 = date.valueOf(); var time3 = Date.parse(date); console.log(time1); //1670145353555 console.log(time2); //1670145353555 console.log(time3); //1670145353000
總結
到此這篇關于微信小程序js時間戳與日期格式的轉換方法的文章就介紹到這了,更多相關微信小程序時間戳與日期轉換內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
JavaScript實現(xiàn)對JSON對象數(shù)組數(shù)據(jù)進行分頁處理
這篇文章主要介紹了使用JavaScript實現(xiàn)對JSON對象數(shù)組數(shù)據(jù)進行分頁處理,文中有詳細的代碼示例供大家參考,對大家的學習或工作有一定的幫助,需要的朋友可以參考下2024-10-10tsc性能優(yōu)化Project References使用詳解
這篇文章主要為大家介紹了tsc性能優(yōu)化Project References使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11jscript之Open an Excel Spreadsheet
jscript之Open an Excel Spreadsheet...2007-06-06