【问题标题】:Date in input is different from the one saved in database输入日期与数据库中保存的日期不同
【发布时间】:2016-12-19 01:29:01
【问题描述】:

我使用类型 date 来获取日期,但我选择的日期和保存在数据库中的日期不同。保存的日期将始终是我选择的日期的前一天。例如,在我的输入中,日期是 8 月 8 日,但在数据库中它保存为 8 月 7 日。我该如何解决这个问题,以及如何在没有 T16:00:00.000Z 的情况下显示日期。

dairy.html

<div>
<label> Date: </label>  
<input type="date" ng-model="diary.date">
</div>

data.html

<table style="width:90%"  align="center" class="t3" ng-controller="RetrieveDiaryController">
      <tr>
        <th style="width:40%">Date</th>
        <th style="width:30%">Type</th>
        <th style="width:30%">Level</th>
      </tr>
      <tr ng-repeat="d in diaries | orderBy:'date':true">
        <td>{{d.date}}</td>
        <td>{{d.taken}}</td>
        <td>{{d.level}}</td>
      </tr>
    </table>

health.js

.

controller('TrackingController', ['$scope', 'Diary', '$state', function($scope, Diary, $state) {
    $scope.diaries = [];

$scope.submitForm = function() {
  Diary
    .upsert({
      date: $scope.diary.date,
      taken: $scope.diary.taken,
      level: $scope.diary.level
    })
    .$promise
    .then(function() {
      $state.go('data');
    });
};
  }])
    .controller('RetrieveDiaryController', ['$scope', 'Diary', function ($scope, Diary) {
       $scope.diaries = Diary.find();
    }]) 

【问题讨论】:

    标签: javascript angularjs node.js strongloop apic


    【解决方案1】:

    试试基本日期过滤器或角度

    <!DOCTYPE html>
    <html>
    	<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
    	<body>
    		<div ng-app="myApp" ng-controller="datCtrl" ng-model="test='2016-08-02T16:00:00.000Z'">
    			<p>Date = {{ test| date }}</p>
    		</div>
    		<script>
    var app = angular.module('myApp', []);
    app.controller('datCtrl', function($scope) {
        $scope.today = new Date();
    });
    </script>
    	</body>
    </html>

    【讨论】:

      【解决方案2】:

      YYYY-MM-DDTHH:mm:ss.SSSUTC 的标准 ISO 日期时间格式。如果你想得到YYYY-MM-DD格式而不转换为当地时间,你可以这样做。

      //used for padding left with 0 on one character strings. e.g. "1" => "01"
      var pad = function(str){ str = str.toString(); return "00".substring(0, 2-str.length) + str; }
      
      var date = new Date("2016-08-02T16:00:00.000Z");
      var utcdatestring = date.getUTCFullYear() + "-" + pad(date.getUTCMonth() + 1) + "-" + pad(date.getUTCDate());
      console.log(utcdatestring); //Logs "2016-08-02"
      

      但是,我强烈建议使用Moment.js,因为在 javascript 中处理日期对于 vanilla javascript 来说确实很麻烦。

      var date = moment.utc("2016-08-02T23:00:00.000Z"); //parse datetime string as utc date
      var utcdatestring = date.format("YYYY-MM-DD"); //use .format() to get date string in any format you want
      console.log(utcdatestring); // Logs "2016-08-02"
      &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.14.1/moment.js"&gt;&lt;/script&gt;

      【讨论】:

        【解决方案3】:

        这是由于日期的时区值以及您输入日期的机器和运行数据库的服务器之间存在一些差异。如果您只想保存简单的日期字符串,请使用 getDay() 或 getFullYear 等 Date 对象方法创建日期字符串,然后将其保存到数据库中。

        【讨论】:

          猜你喜欢
          • 2014-11-19
          • 2022-09-30
          • 2015-05-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-03-16
          • 1970-01-01
          相关资源
          最近更新 更多