Javascript提取月,日和年格式

时间:2015-12-13 18:51:37

标签: javascript angularjs

您好我有以下代码,分别提取日,月和年部分。

  $scope.$watch('inlineDatepicker.date', function() {
       alert('hey, myVar has changed!' +$scope.inlineDatepicker.date);
       alert($scope.inlineDatepicker.date.getDay());
         alert($scope.inlineDatepicker.date.getMonth());
          alert($scope.inlineDatepicker.date.getUTCFullYear());

   });

代码问题是我可以正确提取年份,但是日期和月份无法正确提取。我也试过了

    alert($scope.inlineDatepicker.date.getUTCDady());
     alert($scope.inlineDatepicker.date.getUTCMonth())

日和月仍然错误。 请让我知道如何更改它以获得正确的月和日值。谢谢。这是它的掠夺者。

http://plnkr.co/edit/8v75gsz8ODUrTfu8S0sh?p=preview

2 个答案:

答案 0 :(得分:0)

要获取每月的某一天,请使用getUTCDate()
月份为零,因此0表示1月,12月11日 样品

$scope.inlineDatepicker.date.getUTCDate(); //prints day of month
$scope.inlineDatepicker.date.getUTCMonth() + 1; //prints month, 0 based, so add 1
$scope.inlineDatepicker.date.getUTCFullYear(); //prints 2015

答案 1 :(得分:0)

获取日期返回星期几。使用getDate()返回月中的某天。如果你做了getMonth(),那么1月是0而12月是11。

您可以在此处查看参考文档:http://www.w3schools.com/jsref/jsref_obj_date.asp

相关问题