前端转日期格式

后端的日期一般是定义为Date类型的,前端接收到以后,直接在页面显示的话,会出现问题(格式不对),现在就写一个function来转化一下:

 1 export function getNowDateTime (dateStr) { // 获取当前日期
 2   var date = new Date(dateStr)
 3   var seperator1 = '-'
 4   var year = date.getFullYear()
 5   var month = date.getMonth() + 1
 6   var strDate = date.getDate()
 7   var hour = date.getHours()
 8   var minutes = date.getMinutes()
 9   var seconds = date.getSeconds()
10   if (month >= 1 && month <= 9) {
11     month = '0' + month
12   }
13   if (strDate >= 0 && strDate <= 9) {
14     strDate = '0' + strDate
15   }
16   if (hour >= 0 && hour <= 9) {
17     hour = '0' + hour
18   }
19   if (minutes >= 0 && minutes <= 9) {
20     minutes = '0' + hour
21   }
22   if (seconds >= 0 && seconds <= 9) {
23     seconds = '0' + seconds
24   }
25   var currentdate = year + seperator1 + month + seperator1 + strDate + ' ' + hour + ':' + minutes + ':' + seconds
26   return currentdate
27 };

猜你喜欢

转载自www.cnblogs.com/junehozhao/p/11883121.html