微信小程序wxs格式化日期 在 ios 端顯示NaN問題及日期格式化工具


 1 //timestamp   時間戳
 2 //option      格式(年月日  就輸入YY-MM-DD   時分  就輸入 hh-mm)
 3 //
 4 function formatDate(timestamp, option) {
 5 
 6   var times = timestamp.replace("-", "/").replace("-", "/")
 7   console.log(times)
 8   var date = getDate(times);
 9   var year = date.getFullYear();
10   var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
11   var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
12   var hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
13   var minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
14   var seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
15   var over_time = year + "/" + month + "/" + day + " " + hours + ":" + minutes + ":" + seconds
16   //***至此以上是將時間2020-03-18T01:57:23.000+0000轉為正常時間格式,以下為將時間進行增加8小時解決時區差異的操作***
17   var time = getDate(Date.parse(over_time));
18   time.setTime(time.setHours(time.getHours() + 8));
19 
20   //獲取 年月日
21   if (option == 'YY-MM-DD') return " " + year + "-" + month + "-" + day;
22 
23   //獲取年月
24   if (option == 'YY-MM') return " " + year + "-" + month;
25 
26   //獲取年
27   if (option == 'YY') return " " + year;
28 
29   //獲取月
30   if (option == 'MM') return " " + month;
31 
32   //獲取日
33   if (option == 'DD') return " " + day;
34 
35   //獲取昨天
36   if (option == 'yesterday') return " " + day - 1;
37 
38   //獲取時分秒
39   if (option == 'hh-mm-ss') return " " + hours + ":" + minutes + ":" + seconds;
40 
41   //獲取時分
42   if (option == 'hh-mm') return " " + hours + ":" + minutes;
43 
44   //獲取分秒
45   if (option == 'mm-ss') return minutes + ":" + seconds;
46 
47   //獲取分
48   if (option == 'mm') return minutes;
49 
50   //獲取秒
51   if (option == 'ss') return second;
52 
53   //默認時分秒年月日
54   return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ":" + seconds;
55 }
56 
57 
58 module.exports = {
59   formatDate: formatDate
60 }

ios端顯示NaN的原因是:ios設備不支持new Date(time)的這個time格式為,即:yyyy-mm-dd。我們必須要轉換成"/"格式。而wxs文件不支持new Date,所以我們需要使用getDate


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM