如何用JS将时间设置为T00:00:00.000Z

时间:2015-01-04 19:44:49

标签: javascript angularjs datetime angular-ui

我正在使用ui-calendar而我正试图无时间地传递事件。现在我得到这样的日期和时间。

var date = new Date();
var d = date.getDate();
var m = date.getMonth();
var y = date.getFullYear();
var WOD = new Date(y,m,d);

现在我还有一个功能,可以将WOD更改为日历的月视图中选择的日期。

$scope.alertOnDayClick = function(d){
    WOD = d;
};

如果我点击一天(2015年1月22日这个例子)WOD = "2015-01-22T00:00:00.000Z"< - 这就是我想要的并且运作良好。

但如果我没有默认选择一天WOD = "2015-01-04T05:00:00.000Z"

我想这样做,以便我的默认日期包含时间T00:00:00.000Z。

我试过了

var WOD = new Date(y,m,d,0,0,0,0)
var WOD = date.UTF(y,m,d)
var WOD = date.UTF(y,m,d,0,0,0,0)

我也尝试将默认时间设置为null 我读了这个问题 Dealing with DateTime format for international application 但我不知道这是否适用,老实说我并不完全明白我将如何实施它

感谢所有帮助。

更新:

看完之后 What is the best way to initialize a JavaScript Date to midnight?

我试过

var WOD = new Date();
    WOD.setHours(0,0,0,0);

但它仍会返回"2015-01-04T05:00:00.000Z"

然后将事件存储在1月4日上午12点

更新:

我试过了:

$scope.WOD = new Date();
$scope.WOD.setHours(0, 0, 0, 0);
$scope.dangerMessage = ($scope.WOD);

{{dangerMessage}} = "2015-01-04T05:00:00.000Z"

更新W /完整控制器

var myAppModule = angular.module('MyApp', ['ui.calendar']);
myAppModule.controller('MyController', function($scope,$compile,uiCalendarConfig) {
    var date = new Date();
    var d = date.getDate();
    var m = date.getMonth();
    var y = date.getFullYear();
    $scope.WOD = new Date();
    $scope.WOD.setHours(0, 0, 0, 0);

    $scope.events = [
        {title: 'All Day Event', start: new Date(y, m, 1)},
        {title: 'Long Event', start: new Date(y, m, d), end: new Date(y, m, d)},
        {id: 999, title: 'Repeating Event', start: new Date(y, m, d - 3, 16, 0), allDay: false},
        {id: 999, title: 'Repeating Event', start: new Date(y, m, d + 4, 16, 0), allDay: false},
        {
            title: 'Birthday Party',
            start: new Date(y, m, d + 1, 19, 0),
            end: new Date(y, m, d + 1, 22, 30),
            allDay: false
        },
        {title: 'Click for Google', start: new Date(y, m, 28), end: new Date(y, m, 29), url: 'http://google.com/'}
        ];

    $scope.addEvent = function() {
        if($scope.WOD === ''){
            $scope.dangerMessage = ('Please Choose a Date');
        }else{
            $scope.events.push({
                title: 'Open Sesame',
                start: $scope.WOD,
                end: $scope.WOD,
                className: ['openSesame']
            });
        }
        $scope.dangerMessage = ($scope.WOD);
    };

    $scope.alertOnEventClick = function( date ){
        $scope.alertMessage = (date.title + ' was clicked ');
    };
    $scope.alertOnDayClick = function(d){
        uiCalendarConfig.calendars['CalDayView'].fullCalendar('gotoDate', d);
        WOD = d;
        $scope.dangerMessage = (WOD);
    };
    /* remove event */
    $scope.remove = function(index) {
        $scope.events.splice(index,1);
    };

        /* config object */
    $scope.uiDayConfig = {
        calendar:{
            height: 450,
            editable: true,
            header:{
                left: 'today',
                center: 'title',
                right: 'prev,next'
            },
            timeFormat: '',
            defaultView: 'basicDay',
            defaultDate: new Date(),
            eventDrop: $scope.alertOnDrop,
            eventResize: $scope.alertOnResize,
            eventClick: $scope.alertOnEventClick,
            eventRender: $scope.eventRender
        }
    };
    $scope.uiMonthConfig = {
        calendar:{
            height: 450,
            editable: true,
            header:{
                left: 'prev,next',
                center: 'title',
                right: 'basicWeek'
            },
            dayClick: $scope.alertOnDayClick,
            //eventDrop: $scope.alertOnDrop,
            //eventResize: $scope.alertOnResize,
            eventClick: $scope.alertOnEventClick
            //eventRender: $scope.eventRender
        }
    };

    $scope.eventSources = [$scope.events];

});//END MYCONTROLLER

2 个答案:

答案 0 :(得分:1)

我认为你在时区里有+5小时。

var WOD = new Date();
    WOD.setHours(0,0,0,0);// Sun Jan 04 2015 00:00:00 GMT-0600 (CST)

var WOD = new Date();
    WOD.setUTCHours(0,0,0,0);// Sat Jan 03 2015 18:00:00 GMT-0600 (CST)

您的机器设置的所有差异。检查"时区"。

的设置

答案 1 :(得分:0)

请参阅下面的演示;



var app = angular.module('app', []);

app.controller('firstCtrl', function($scope) {

  $scope.date = new Date();

  $scope.date.setHours(0, 0, 0, 0);

});

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<body ng-app="app">
  <div ng-controller="firstCtrl">
    {{date}}
  </div>
</body>
&#13;
&#13;
&#13;