javaScript 时间转换,将后台返回的时间为一串数字转成正常格式

js完整代码:

function transferTime(cTime){       var jsonDate = new Date(parseInt(cTime));       Date.prototype.format = function (format){             var  o = {                  "y+": this.getFullYear(),                  "M+": this.getMonth()+1,                  "d+": this.getDate(),                  "h+": this.getHours(),                  "m+": this.getMinutes(),                  "s+": this.getSeconds()
             };

             if(/(y+)/.test(format)){                  format = format.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));             }

             for(var k in o){                  if(new RegExp("("+k+")").test(format)){                      format = format.replace(RegExp.$1, RegExp.$1.length == 1?o[k] : ("00" + o[k]).substr("" + o[k].length));                  }             }             return format;       };       var newDate = jsonDate.format("yyyy-MM-dd hh:mm:ss");       return newDate
}

Trust me!!

原文地址:https://www.cnblogs.com/wuliMan/p/10501724.html

时间: 2024-08-04 19:08:09

javaScript 时间转换,将后台返回的时间为一串数字转成正常格式的相关文章

iOS开发中如何将后台返回的时间转换为常见字串

在iOS开发中,经常需要从后台数据中获取时间,但是此时后台返回的时间格式经常是一串类似"123456789"的数字,此数字是1970年开始截至到现在的秒数,此时我们需要将其转换为可以用的格式. 模型中我定义了c_time属性,通过重写c_time的get方法中进行格式的转换. - (NSString *)c_time{ // 1.创建时间格式化类 NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; // 2.设置时间

时间格式转换—将后台返回的/Date(1448954018000)/格式转换为正常的时间格式

用JS实现方法: function ChangeDateFormat(cellval) { var date = new Date(parseInt(cellval.replace("/Date(", "").replace(")/", ""), 10)); var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : dat

使用freemarker模板引擎,后台返回的时间格式(datetime类型)无法显示在input type=&quot;text&quot;中显示

详情: 在使用freemarker是,在后台查出user,其中有入职时间entryDate(date类型),当我想要将 entryDate 显示在 <input type="text" value="${user.entryDate}"/>时,出现错误,无法显示 显示页面: 原因: 因为freemarker无法确定用那种格式来显示这个时间 解决办法: 在 <input type="text" value="${user

前端传一个时间,在后台中比较时间大小的操作

public void getOperatingImcomeDataList(){ SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date state=DateUtil.getDateByString(beginTime,DateUtil.DATE_PATTERN_23); Date edate=DateUtil.getDateByString(endTime,DateUtil.DATE_PAT

js把字符串格式的时间转换成几秒前、几分钟前、几小时前、几天前等格式

最近在做项目的时候,需要把后台返回的时间转换成几秒前.几分钟前.几小时前.几天前等的格式:后台返回的时间格式为:2015-07-30 09:36:10,需要根据当前的时间与返回的时间进行对比,最后显示成几秒前.几分钟前.几小时前.几天前的形式. 1.由于返回的时间是字符串格式,所以要先转换成时间戳 //字符串转换为时间戳 function getDateTimeStamp (dateStr) { return Date.parse(dateStr.replace(/-/gi,"/"))

返回/设置时间方法

返回/设置时间方法 get/setTime() 返回/设置时间,单位毫秒数,计算从 1970 年 1 月 1 日零时到日期对象所指的日期的毫秒数. 如果将目前日期对象的时间推迟1小时,代码如下: <script type="text/javascript"> var mydate=new Date(); document.write("当前时间:"+mydate+"<br>"); mydate.setTime(mydate

mysql中返回当前时间的函数或者常量

引用:http://blog.sina.com.cn/s/blog_6d39dc6f0100m7eo.html 1.1 获得当前日期+时间(date + time)函数:now() 除了 now() 函数能获得当前的日期时间外,MySQL 中还有下面的函数: current_timestamp()   current_timestamp localtime()   localtime localtimestamp()   localtimestamp     这些日期时间函数,都等同于 now(

mysql 日期操作 增减天数、时间转换、时间戳

MySQL datediff(date1,date2):两个日期相减 date1 - date2,返回天数.select datediff('2008-08-08', '2008-08-01'); -- 7select datediff('2008-08-01', '2008-08-08'); -- -7 一.MySQL 获得当前日期时间 函数1.1 获得当前日期+时间(date + time)函数:now()mysql> select now();+---------------------+

mysql 日期操作 增减天数、时间转换、时间戳(转)

转自http://www.cnblogs.com/wenzichiqingwa/archive/2013/03/05/2944485.html http://hi.baidu.com/juntao_li/item/094d78c6ce1aa060f6c95d0b MySQL datediff(date1,date2):两个日期相减 date1 - date2,返回天数.select datediff('2008-08-08', '2008-08-01'); -- 7select datediff