简体   繁体   English

如何使用javascript或jquery将日期转换为JSON格式?

[英]How to convert a date to JSON format using javascript or jquery?

How can I convert a date to JSON format using JavaScript or jQuery? 如何使用JavaScript或jQuery将日期转换为JSON格式?

For example: 28/08/2013 or Wed Aug 28 2013 12:09:29 GMT+0530 to: 例如:28/08/2013星期三2013年8月28日12时09分29秒GMT + 0530到:

Date(928129800000+0530) 日期(928129800000 + 0530)

How can I convert a date to JSON format 如何将日期转换为JSON格式

JSON has no format for dates. JSON没有日期格式。 That string you frequently see is a popular convention , nothing more. 您经常看到的字符串是一个流行的约定 ,仅此而已。 (And like many conventions, there are variations on that theme.) (和许多约定一样,该主题也有所不同。)

Looking at it, it's the underlying Epoch value plus the timezone offset. 看着它,它是底层的Epoch值加上时区偏移量。 Both of those pieces of information are available from the Date object. 这两个信息都可以从Date对象获得。 The Epoch value comes from getTime , and the timezone offset from getTimezoneOffset (which is in minutes). Epoch值来自getTime ,并且时区偏离getTimezoneOffset (以分钟为单位)。

Using those, you can create a string in that form. 使用这些,可以创建该形式的字符串。

what about this, 那这个呢,

I have Edited the code... pls try this, 我已经编辑了代码...请尝试一下,

public class DateHandler : IHttpHandler  
{  

    public void ProcessRequest(HttpContext context)  
    {  
        context.Response.ContentType = "application/json";  
        string action = context.Request.QueryString["jsonmode"];  
        string json = null;  

        if (!string.IsNullOrEmpty(action) && action == ".net")  
        {  
            // Creates date in .NET date format "\/Date(14123123132)\/"  
            JavaScriptSerializer ser = new JavaScriptSerializer();  
            json = ser.Serialize(DateTime.Now);  
        }  
        else  
            // iso format: "2010-08-31T01:35:05.785Z"  
            json = "\"" + DateTime.Now.ToUniversalTime().ToString("s") +   
                   "Z" + "\"";  

        context.Response.Write(json);   
    }  

    public bool IsReusable  
    {  
        get { return false; }  
    }  
}  

Try the following simple method 试试下面的简单方法

var dateString= "/Date(1224043200000)/"; var dateString =“ / Date(1224043200000)/”; var date= new Date(parseInt(dateString.substr(6))); var date = new Date(parseInt(dateString.substr(6)));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM