zoukankan      html  css  js  c++  java
  • C#返回时间格式转换成 js 字符串

    在.net 中,调用 post 或者 get和后台通信时,如果有时间返回信息,后台返回的时间信息一般是这样格式:Thu Jul 9 23:14:53 UTC+0800 2015,那么要在前台显示就会有点点小困难。最近使用到,小小研究了下,参照网络中的写法,解决方法如下:
    1.先将C#的格式转换成js 的datetime形式。

    /*
    *将C#返回的日期格式进行转换 将 类似 Thu Jul 9 23:14:53 UTC+0800 2015 转换成 2015-05-06
    *Parameters:
    *orgData - {string} C# 日期 格式  //Thu Jul 9 23:14:53 UTC+0800 2015
    *targetFormate - {string} 目标日期格式 yyyy/MM/dd之类的
    *Returns:{string} 格式化后的日期  类似2015-2-3
    */
    MyDemo.Util.CSharpDataConvert = function (orgData, formate) {
        orgData = orgData + "";
        var date = "";
        var month = new Array();
        month["Jan"] = 1; month["Feb"] = 2; month["Mar"] = 3; month["Apr"] = 4; month["May"] = 5; month["Jan"] = 6;
        month["Jul"] = 7; month["Aug"] = 8; month["Sep"] = 9; month["Oct"] = 10; month["Nov"] = 11; month["Dec"] = 12;
        var week = new Array();
        week["Mon"] = "一"; week["Tue"] = "二"; week["Wed"] = "三"; week["Thu"] = "四"; week["Fri"] = "五"; week["Sat"] = "六"; week["Sun"] = "日";
        str = orgData.split(" ");
        date = str[5] + "/";
        date = date + month[str[1]] + "/" + str[2];
        var tempData = new Date(date);
        if (!formate) { formate = 'yyyy-MM-dd'; }
        return tempData.format(formate);
    };
    

      

    2.拓展js Date方法。得到格式化的日期形式 。

    /*
    *拓展Date方法。得到格式化的日期形式 基本是什么格式都支持
    *date.format('yyyy-MM-dd'),date.format('yyyy/MM/dd'),date.format('yyyy.MM.dd')
    *date.format('dd.MM.yy'), date.format('yyyy.dd.MM'), date.format('yyyy-MM-dd HH:mm')   等等都可以
    *使用方法 如下:
    *                       var date = new Date();
    *                       var todayFormat = date.format('yyyy-MM-dd'); //结果为2015-2-3
    *Parameters:
    *format - {string} 目标格式 类似('yyyy-MM-dd')
    *Returns - {string} 格式化后的日期 2015-2-3
    *
    */
    Date.prototype.format = function (format) {
        var o = {
            "M+": this.getMonth() + 1, //month
            "d+": this.getDate(), //day
            "h+": this.getHours(), //hour
            "m+": this.getMinutes(), //minute
            "s+": this.getSeconds(), //second
            "q+": Math.floor((this.getMonth() + 3) / 3), //quarter
            "S": this.getMilliseconds() //millisecond
        }
        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;
    }
    

      

  • 相关阅读:
    CSS大杂烩(1)
    菜鸟快飞之JavaScript函数
    mysqldump主要参数分析
    PXC原理分析
    MHA原理分析
    android数据存储之外部存储(External Storage)
    JsonWriter使用
    基于asp.net mvc的近乎产品开发培训课程(第二讲)
    基于asp.net mvc的近乎产品开发培训课程(第一讲)
    MVC的注意事项及开发技巧
  • 原文地址:https://www.cnblogs.com/airbreak/p/4640608.html
Copyright © 2011-2022 走看看