如何处理日期为服务器发布施行 [英] how to handle date for posting purposes in the server

查看:201
本文介绍了如何处理日期为服务器发布施行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从ASP.NET MVC /日期(1446393600000)/ 这它在SQL数据库值2015年11月2日该值这是一个日期(仅按日期),并把它作为操作的JavaScript对象,我用的 moment.js ,从而时刻使用时,('/日期(1446393600000)/')我得到这个结果2015-11-01T16:00:00.000Z

所以这code:

该bindingHandler可以发现这里

\r
\r

ko.bindingHandlers.datepicker = {\r
  初始化:功能(元素,valueAccessor,allBindingsAccessor){\r
    VAR的选择= allBindingsAccessor()。datepickerOptions || {},\r
        $ EL = $(元);\r
\r
    //某些可选选项初始化日期选择器\r
    $ el.datepicker(选件);\r
\r
    //处理领域的变化\r
    ko.utils.registerEventHandler(元素,变,函数(){\r
      无功可观察= valueAccessor();\r
      可观察到的(矩($ el.datepicker(GETDATE))); //我在这里补充矩函数\r
    });\r
\r
    //处理处置(如KO删除由模板绑定)\r
    ko.utils.domNodeDisposal.addDisposeCallback(元素,函数(){\r
      $ el.datepicker(消灭);\r
    });\r
\r
  }\r
  / *,\r
    更新:功能(元素,valueAccessor){\r
      VAR值= ko.utils.unwrapObservable(valueAccessor()),\r
        $ EL = $(元素),\r
        电流= $ el.datepicker(GETDATE);\r
\r
      如果(值! - 当前== 0){\r
        $ el.datepicker(的setDate,值);\r
      }\r
    } * /\r
};\r
  \r
VAR VM = {\r
  sampleDate:ko.observable(矩('/日期(1446393600000)/'))//我在这里补充矩函数\r
  originalValue:'/日期(1446393600000)/',//仅供参考\r
  FromSqlDb:2015年11月2日//仅供参考\r
};\r
\r
vm.formattedDate = ko.pureComputed(函数(){\r
  返回this.sampleDate()?\r
    this.sampleDate()格式(DDD,DD MMM YYYY):\r
  '';\r
},VM);\r
 \r
\r
ko.applyBindings(VM);

\r

&LT;脚本SRC =htt​​ps://cdnjs.cloudflare.com/ajax /libs/moment.js/2.10.6/moment.min.js\"></script>\r
&LT;链接HREF =htt​​ps://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.css的rel =stylesheet属性/&GT;\r
&所述; SCRIPT SRC =htt​​ps://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js&GT;&下; /脚本&GT;\r
&LT;脚本的src =// code.jquery.com / UI / 1.11.4 / jQuery的-ui.js&GT;&LT; / SCRIPT&GT;\r
&所述; SCRIPT SRC =htt​​ps://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js&GT;&下; /脚本&GT;\r
\r
&LT;输入类型=文本数据绑定=日期选择器:sampleDate/&GT; &LT; BR /&GT;\r
&LT; pre数据绑定=TEXT:ko.toJSON($根,空,2)&GT;&LT; / pre&GT;

\r

\r
\r

正如你所看到的,值 2015年11月2日现在转换成 UTC 2015-11 -01T16:00:00.000Z当我用时刻()就可以了。这是我不想,因为我想保留我从数据库得到了

所以我的问题是:


  1. 是否momentjs永远是您的日期转换为UTC的?

  2. 当我尝试 sampleDate 我得到这个错误。

我的临时解决方案是计算变量。 ( formattedDate 和它的作品)

请帮我理解为什么发生错误。任何帮助将是非常美联社preciated感谢!


解决方案

  1. 日期是你选择什么。用片刻的 TODATE ,你就会得到它具有期望值一个js Date对象。你看到的是UTC再presentation,即同一时间转换为在Z区(经络0,或Greenwrich经络)。


  2. Accordint到OP评论,日期是通过发布 .toISOString()。在这种情况下,如果在比Z(格林威治子午线)以外的时区是,日期将参考previous日期。例如日期二零一五年四月二十零日GMT + 1 ,将被格式化为 2015-04-19T23:00:00.000Z ,并且服务器将片段的时间和时区的信息,所以它会所需的前一天获得的一天。


要避免2.我们可以指示时刻解析日期,如果它是一个UTC日期,像这样的问题: moment.utc('datestring') 。例如,在做 moment.utc('2015-03-11')将创建日期 2015-03-11T00:00:00.000Z ,所以covnersion到ISO字符串将得到所需要的日期。在这种情况下,你必须改变这一行code的:

 可观测(瞬间($ el.datepicker(GETDATE)))

 观察到的(moment.utc($ el.datepicker(GETDATE)))

I have this value from ASP.NET MVC /Date(1446393600000)/ which its value in SQL Database is '2015-11-02' which is a date (date only) and to manipulate it as javascript object, I used moment.js, so when used as moment('/Date(1446393600000)/') I got this result "2015-11-01T16:00:00.000Z"

And so this code:

The bindingHandler can be found here

ko.bindingHandlers.datepicker = {
  init: function(element, valueAccessor, allBindingsAccessor) {
    var options = allBindingsAccessor().datepickerOptions || {},
        $el = $(element);

    //initialize datepicker with some optional options
    $el.datepicker(options);

    //handle the field changing
    ko.utils.registerEventHandler(element, "change", function() {
      var observable = valueAccessor();
      observable(moment($el.datepicker("getDate"))); // I added moment function here
    });

    //handle disposal (if KO removes by the template binding)
    ko.utils.domNodeDisposal.addDisposeCallback(element, function() {
      $el.datepicker("destroy");
    });

  }
  /*,
    update: function(element, valueAccessor) {
      var value = ko.utils.unwrapObservable(valueAccessor()),
        $el = $(element),
        current = $el.datepicker("getDate");

      if (value - current !== 0) {
        $el.datepicker("setDate", value);
      }
    }*/
};
  
var vm = {
  sampleDate: ko.observable(moment('/Date(1446393600000)/')), // I added moment function here
  originalValue: '/Date(1446393600000)/', // For reference only
  FromSqlDb: '2015-11-02' // For reference only
};

vm.formattedDate = ko.pureComputed(function() {
  return this.sampleDate() ? 
    this.sampleDate().format("ddd, DD MMM YYYY") :
  '';
}, vm);
 

ko.applyBindings(vm);

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.6/moment.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>

<input type="text" data-bind="datepicker: sampleDate" /> <br />
<pre data-bind="text: ko.toJSON($root, null, 2)"></pre>

As you can see, the value 2015-11-02 is now converted to UTC "2015-11-01T16:00:00.000Z" when I used moment() on it. Which is I don't want, since I want to retain what I have got from the db

So my question is:

  1. Does momentjs always convert your date to its utc?
  2. When I try to post the sampleDateI got this error.

My temporary solution is to post the computed variable. (formattedDate and it works)

Please help me understand why the error occur. Any help would be much appreciated thanks!

解决方案

  1. The date is what you chose. Use moment's toDate and you'll get a js Date object which has the expected value. What you see is the UTC representation, i.e. the same hour translated to the Z zone (meridian 0, or Greenwrich meridian).

  2. Accordint to the OP comment, the date is posted by using .toISOString(). In this case, if you're in a time zone other than Z (Greenwich meridian), the date will refer to the previous date. For example the date 2015-04-20 GMT+1, will be formatted as 2015-04-19T23:00:00.000Z, and the server will clip the time and time zone info, so it will get one day before the desired day.

To avoid the problem in 2. we can instruct moment to parse the date as if it was an UTC date, like this: moment.utc('datestring'). For example, doing moment.utc('2015-03-11') will create the date 2015-03-11T00:00:00.000Z, so the covnersion to ISO string will give the desired date. In this instance, you have to change this line of code:

observable(moment($el.datepicker("getDate")))

to

observable(moment.utc($el.datepicker("getDate")))

这篇关于如何处理日期为服务器发布施行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆