当前位置: 移动技术网 > IT编程>开发语言>JavaScript > React Native时间转换格式工具类分享

React Native时间转换格式工具类分享

2017年12月12日  | 移动技术网IT编程  | 我要评论
本文实例为大家分享了react native时间转换格式工具类,供大家参考,具体内容如下 class dateutil{ /** * 例如:2017

本文实例为大家分享了react native时间转换格式工具类,供大家参考,具体内容如下

class dateutil{
  /**
   * 例如:2017-06-28 10:48:46转成date类,
   * 可把- replace成/
   * @param datestring
   * @return date
   */
  static parserdatestring(datestring){
    if(datestring){
      let regex = new regexp("\\-","gi");
      let validdatestr=datestring.replace(regex,"/");
      let milliseconds=date.parse(validdatestr);
      return new date(milliseconds);

    }
  }

  // timestamp时间戳 formater时间格式
  static formatdate(timestamp, formater) { 
    let date = new date();
    date.settime(parseint(timestamp));
    formater = (formater != null)? formater : 'yyyy-mm-dd hh:mm';
    date.prototype.format = function (fmt) {
      var o = {
        "m+": this.getmonth() + 1, //月
        "d+": this.getdate(), //日
        "h+": this.gethours(), //小时
        "m+": this.getminutes(), //分
        "s+": this.getseconds(), //秒
        "q+": math.floor((this.getmonth() + 3) / 3), //季度
        "s": this.getmilliseconds() //毫秒
      };

      if (/(y+)/.test(fmt)) fmt = fmt.replace(regexp.$1, (this.getfullyear() + "").substr(4 - regexp.$1.length));
      for (var k in o) {
        if (new regexp("(" + k + ")").test(fmt)) fmt = fmt.replace(regexp.$1, (regexp.$1.length == 1) ?
          (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
      }
      return fmt;
    }
    return date.format(formater);
  }
}
export default dateutil;


外界调用方法

// 可将 - 替换成 / 或者 年月日等
dateutil.formatdate(时间戳, "yyyy-mm-dd hh:mm:ss")

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网