日期转换 .NET JSON 到 ISO

Date conversion .NET JSON to ISO(日期转换 .NET JSON 到 ISO)
本文介绍了日期转换 .NET JSON 到 ISO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

如何从 JSON.Net 转换日期时间格式,例如:

How can I convert a date time format from JSON.Net such as:

/日期(1154970000000+0700)/

/Date(1154970000000+0700)/

到 ISO-??格式2011-12-18T23:34:59Z

To ISO-?? format 2011-12-18T23:34:59Z

最好使用 Python 或 Javascript.

Preferably in either Python or Javascript.

我决定选择后者,因为它在 JS 世界中似乎是使用最广泛、可读性最强且自然可排序的.我将按用户存储偏移量.

Ive decided on the latter as its seems in the JS world the most widely used, humanly readable and naturally sortable. I'll store offsets on a per user basis.

如果一个实现有点太问了,如果有人能告诉我这两种格式的正确名称,我可能会更幸运地理解如何转换.

If an implementation is again a bit much too ask, if someone can tell me the correct name for both formats I might have more luck in understanding how to convert.

推荐答案

jsonDate = "/Date(1154970000000+0700)/";

var strDate = parseInt(jsonDate.replace(//Date(([-d]+).*$/, "$1"));
var strHour = parseInt(jsonDate.replace(/.*d([+-]dd).*$/, "$1"), 10);
var strMin = parseInt(jsonDate.replace(/.*d([+-])dd(dd).*$/, "$1$2"), 10);

var date = new Date(strDate);
if (!isNaN(strHour)) date.setHours(date.getHours() + strHour);
if (!isNaN(strMin)) date.setMinutes(date.getMinutes() + strMin);

var out = date.toISOString();

以及转换为 ISO 的函数:

And the function to convert to ISO:

var toISOString = Date.prototype.toISOString ?
    function(d){return d}:
    (function(){
        function t(i){return i<10?"0"+i:i};
        function h(i){return i.length<2?"00"+i:i.length<3?"0"+i:3<i.length?Math.round(i/Math.pow(10,i.length-3)):i};
        function toISOString(){
            return "".concat(
                this.getUTCFullYear(), "-",
                t(this.getUTCMonth() + 1), "-",
                t(this.getUTCDate()), "T",
                t(this.getUTCHours()), ":",
                t(this.getUTCMinutes()), ":",
                t(this.getUTCSeconds()), ".",
                h("" + this.getUTCMilliseconds()), "Z"
            );
        };
        return function(d){
            d.toISOString = toISOString;
            return d;
        }
    })();

这篇关于日期转换 .NET JSON 到 ISO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Drag amp; Drop with Protractor by Repeater(拖动amp;通过中继器使用量角器掉落)
Getting the position of the element in a list when it#39;s drag/dropped (ui.sortable)(拖放时获取元素在列表中的位置(ui.sortable))
Detecting HTML5 Drag And Drop support in javascript(在 javascript 中检测 HTML5 拖放支持)
HTML5 drop event doesn#39;t work unless dragover is handled(除非处理了拖动,否则 HTML5 放置事件不起作用)
How to use jQuery#39;s drop event to upload files dragged from the desktop?(如何使用 jQuery 的 drop 事件上传从桌面拖动的文件?)
Drop image into contenteditable in Chrome to the cursor(将图像拖放到 Chrome 中的 contenteditable 到光标处)