React Native时间转换格式工具类分享
本文实例为大家分享了ReactNative时间转换格式工具类,供大家参考,具体内容如下
classDateUtil{
/**
*例如:2017-06-2810:48:46转成date类,
*可把-replace成/
*@paramdateString
*@returnDate
*/
staticparserDateString(dateString){
if(dateString){
letregEx=newRegExp("\\-","gi");
letvalidDateStr=dateString.replace(regEx,"/");
letmilliseconds=Date.parse(validDateStr);
returnnewDate(milliseconds);
}
}
//timestamp时间戳formater时间格式
staticformatDate(timestamp,formater){
letdate=newDate();
date.setTime(parseInt(timestamp));
formater=(formater!=null)?formater:'yyyy-MM-ddhh:mm';
Date.prototype.Format=function(fmt){
varo={
"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(varkino){
if(newRegExp("("+k+")").test(fmt))fmt=fmt.replace(RegExp.$1,(RegExp.$1.length==1)?
(o[k]):(("00"+o[k]).substr((""+o[k]).length)));
}
returnfmt;
}
returndate.Format(formater);
}
}
exportdefaultDateUtil;
外界调用方法
//可将-替换成/或者年月日等 DateUtil.formatDate(时间戳,"yyyy-MM-ddhh:mm:ss")
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。