Reputation: 21764
I know that I can get the month from a date object using:
var d = new Date();
var n = d.getMonth();
But in my code I have a $scope.start
property that seems to not be a date object. It is:
2016-02-17T14:39:00Z
How can I extract the month from $scope.start
?
Upvotes: 3
Views: 13211
Reputation: 83
You should be able to do it by using split ! Try something like this
$scope.start = "2016-02-17T14:39:00Z";
{{start.split('-')[0]}}
now you will get, "Month" while you try "{{start.split('-')[1]}}"
Upvotes: 1
Reputation: 1652
But Normally 0 means jan, 1 -> feb, 2-> mar,... so only we did some +1, -1...
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Get Previous next Day, Month, Year Month Time in Angularjs</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.9/angular.min.js"></script>
<script type="text/javascript">
var app = angular.module('sampleapp', [])
app.controller('samplecontrol', function ($scope) {
var previousMonth = new Date()
$scope.pmonth = previousMonth.getMonth();
var currentMonth = new Date()
$scope.cmonth = currentMonth.getMonth() + 1;
var nextMonth = new Date()
$scope.nmonth = nextMonth.getMonth() + 2;
});
</script>
</head>
<body data-ng-app="sampleapp" data-ng-controller="samplecontrol">
<form id="form1">
<div>
Previous Month:<b> {{pmonth }}</b><br />
Current Month:<b> {{cmonth }}</b><br />
Next Month:<b> {{nmonth }}</b><br />
</div>
</form>
</body>
</html>
Upvotes: 0
Reputation: 447
May I recommend the following approach
var n = new Date($scope.start);
var month = n.getMonth();
You need to convert the string to a date object.
Upvotes: 1
Reputation: 1154
May be this will help you pass the date string to Date() constructor.
now the date object is of date contain in string and if u print month or alert it will give u month-1 value.
here is code. please have a look on it
var d = new Date('2016-02-17T14:39:00Z');
var n = d.getMonth();
alert(n);
var d = new Date('2016-03-17T14:39:00Z');
var n = d.getMonth();
alert(n)
Upvotes: 1
Reputation: 1609
$scope.start = new Date('2016-02-17T14:39:00Z');
$scope.startMonth= $scope.start.getMonth() + 1;
http://jsfiddle.net/ms403Ly8/57/
Upvotes: 2
Reputation: 6240
This will give you a month as a number from 0 to 11
var date = new Date('2016-12-17T14:39:00Z');
var month = date.getMonth();
Upvotes: 8