小程序 苹果IOS 时间格式化的问题?nan/nan/nan/nannan:nan

在写小程序的时间格式化时,出现了渲染 nan/nan/nan/nannan:nan 的BUG,
找了很长时间,没有找到原因,查阅了官方帮助文档。
也有百度上查找问题,报错的写法是这样如下:

 onLoad: function (options) {
    var time = new Date(2017, 6, 14, 10, 40, 0, 0);
 
    this.setData({
      time: time
    })
  },

=================================================================
而最终找到原因,对于time 要进行toLocaleDateString()处理。
 onLoad: function (options) {
    var times = new Date(2017, 6, 14, 10, 40, 0, 0);
    var time = times.toLocaleDateString();

    this.setData({
      time: time
    })
  },
demo.js

Page({

  /**
   * 页面的初始数据
   */
  data: {
    day: 5
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var times = new Date(2017, 6, 14, 10, 40, 0, 0);
    var time = times.toLocaleDateString();

    this.setData({
      time: time
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})
demo.wxml

<wxs src="index.wxs" module="m" />
<wxs src="too.wxs" module="tools" />
<view>{{m.getWeekDay(day)}}</view>

<view>{{tools.timeFormat(time)}}</view>
index.wxs


var tools = require("too.wxs");

var getWeekDay = function (day) {
  var weekdays = tools.weekdays;
  // 1-7
  if(day < 1 || day > 7){
    return "错误的日期";
  }else{
    return weekdays[day-1]
  }
  // var weekday = "";
  // switch (day) {
  //   case 1:
  //     weekday = '星期一';
  //     break;
  //   case 2:
  //     weekday = '星期二';
  //     break;
  //   case 3:
  //     weekday = '星期三';
  //     break;
  //   case 4:
  //     weekday = '星期四';
  //     break;
  //   case 5:
  //     weekday = '星期五';
  //     break;
  //   case 6:
  //     weekday = '星期六';
  //     break;
  //   case 7:
  //     weekday = '星期天';
  //     break;
  //   default:
  //     weekday = '错误的日期';
  // }
  return weekdays;
}

var username = 'zhiliao'
function hello(){
  var username;
  console.log(username);
  username = '课堂';
}


hello(); 

module.exports.getWeekDay = getWeekDay;
too.wxs

var weekdays = [
  '星期一',
  '星期二',
  '星期三',
  '星期四',
  '星期五',
  '星期六',
  '星期天'
]


a = 10;
// console.log(a);

var timeFormat = function(time){
  var date = getDate(time);
  var date_seconds = date.getTime() / 1000;
  var now = getDate();
  var now_seconds = now.getTime() / 1000;
  var timestamp = now_seconds - date_seconds;
  var timeStr = "";
  if(timestamp < 60){
    timeStr = "刚刚";
  }else if(timestamp >= 60 && timestamp < 60*60){
    var minutes = parseInt(timestamp / 60);
    timeStr = minutes + "分钟前";
  }else if(timestamp >= 60*60 && timestamp < 60*60*24){
    var hours = parseInt(timestamp / 60 / 60);
    timeStr = hours + "小时前";
  }else if(timestamp >= 60*60*24 && timestamp < 60*60*24*30){
    var days = parseInt(timestamp / 60 /60 /24);
    timeStr = days + "天前";
  }else{
    var year = date.getFullYear();
    var month = date.getMonth();
    var day = date.getDate();
    var hour = date.getHours();
    var minute = date.getMinutes();
    timeStr = year + "/" + month + "/" + day + " " + hour + ":" + minute;
  }
  return timeStr;
}

module.exports = {
  timeFormat: timeFormat,
  weekdays: weekdays
}

发表评论

邮箱地址不会被公开。