zoukankan      html  css  js  c++  java
  • js 将long日期格式 转换为标准日期格式方法

    我们经常在操作的时候会发现从后台传递到view层的json中datetime类型变成了long型,当然你也可以从后台先转为string类型,但是如果是从和数据库对应的object中封装的话,就不能再去转为string类型了,所以需要在js中进行序列化为我们常见的标准日期格式。

    直接上代码,希望可以帮助到大家(另:本人近几天开始做一个 MVC4.0+WCF+EF+Bootstrap的架构系列博文,希望大家支持和指正):

     3 <script language="javascript">
     4 //扩展Date的format方法
     5 Date.prototype.format = function (format) {
     6 var o = {
     7 "M+": this.getMonth() + 1,
     8 "d+": this.getDate(),
     9 "h+": this.getHours(),
    10 "m+": this.getMinutes(),
    11 "s+": this.getSeconds(),
    12 "q+": Math.floor((this.getMonth() + 3) / 3),
    13 "S": this.getMilliseconds()
    14 }
    15 if (/(y+)/.test(format)) {
    16 format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    17 }
    18 for (var k in o) {
    19 if (new RegExp("(" + k + ")").test(format)) {
    20 format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
    21 }
    22 }
    23 return format;
    24 }
    25 /**
    26 *转换日期对象为日期字符串
    27 * @param date 日期对象
    28 * @param isFull 是否为完整的日期数据,
    29 * 为true时, 格式如"2013-12-06 01:05:04"
    30 * 为false时, 格式如 "2013-12-06"
    31 * @return 符合要求的日期字符串
    32 */
    33 function getSmpFormatDate(date, isFull) {
    34 var pattern = "";
    35 if (isFull == true || isFull == undefined) {
    36 pattern = "yyyy-MM-dd hh:mm:ss";
    37 } else {
    38 pattern = "yyyy-MM-dd";
    39 }
    40 return getFormatDate(date, pattern);
    41 }
    42 /**
    43 *转换当前日期对象为日期字符串
    44 * @param date 日期对象
    45 * @param isFull 是否为完整的日期数据,
    46 * 为true时, 格式如"2013-12-06 01:05:04"
    47 * 为false时, 格式如 "2013-12-06"
    48 * @return 符合要求的日期字符串
    49 */
    50 function getSmpFormatNowDate(isFull) {
    51 return getSmpFormatDate(new Date(), isFull);
    52 }
    53 /**
    54 *转换long值为日期字符串
    55 * @param l long值
    56 * @param isFull 是否为完整的日期数据,
    57 * 为true时, 格式如"2013-12-06 01:05:04"
    58 * 为false时, 格式如 "2013-12-06"
    59 * @return 符合要求的日期字符串
    60 */
    61 function getSmpFormatDateByLong(l, isFull) {
    62 return getSmpFormatDate(new Date(l), isFull);
    63 }
    64 /**
    65 *转换long值为日期字符串
    66 * @param l long值
    67 * @param pattern 格式字符串,例如:yyyy-MM-dd hh:mm:ss
    68 * @return 符合要求的日期字符串
    69 */
    70 function getFormatDateByLong(l, pattern) {
    71 return getFormatDate(new Date(l), pattern);
    72 }
    73 /**
    74 *转换日期对象为日期字符串
    75 * @param l long值
    76 * @param pattern 格式字符串,例如:yyyy-MM-dd hh:mm:ss
    77 * @return 符合要求的日期字符串
    78 */
    79 function getFormatDate(date, pattern) {
    80 if (date == undefined) {
    81 date = new Date();
    82 }
    83 if (pattern == undefined) {
    84 pattern = "yyyy-MM-dd hh:mm:ss";
    85 }
    86 return date.format(pattern);
    87 }
    88 //alert(getSmpFormatDate(new Date(1279849429000), true));
    89 //alert(getSmpFormatDate(new Date(1279849429000),false));
    90 //alert(getSmpFormatDateByLong(1279829423000, true));
    91 alert(getSmpFormatDateByLong(1279829423000,false));
    92 //alert(getFormatDateByLong(1279829423000, "yyyy-MM"));
    93 //alert(getFormatDate(new Date(1279829423000), "yy-MM"));
    94 //alert(getFormatDateByLong(1279849429000, "yyyy-MM hh:mm"));
    95 </script> 
  • 相关阅读:
    去除字符串中多余空格
    day02-03 字符编码
    eclipse与myeclipse区别
    xz解压和zip解压
    yum安装
    防火墙
    查看和操作HBA卡
    复制linux虚拟机后网卡不能用的解决方法
    关闭IPv6
    更改root密码
  • 原文地址:https://www.cnblogs.com/merlinhome/p/3460933.html
Copyright © 2011-2022 走看看