最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Get month from date string? - Stack Overflow

programmeradmin6浏览0评论

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?

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?

Share Improve this question asked Feb 18, 2016 at 8:56 user1283776user1283776 21.8k57 gold badges154 silver badges292 bronze badges 2
  • have you tried using the data filter? docs.angularjs/api/ng/filter/date – haxtbh Commented Feb 18, 2016 at 9:00
  • 1 Check for the Date constructors here: developer.mozilla/en-US/docs/Web/JavaScript/Reference/… – ManojAnavatti Commented Feb 18, 2016 at 9:03
Add a ment  | 

6 Answers 6

Reset to default 8

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();
$scope.start = new Date('2016-02-17T14:39:00Z');
$scope.startMonth= $scope.start.getMonth() + 1;

http://jsfiddle/ms403Ly8/57/

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)

May I remend the following approach

var n = new Date($scope.start);
var month = n.getMonth();

You need to convert the string to a date object.

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]}}"

But Normally 0 means jan, 1 -> feb, 2-> mar,... so only we did some +1, -1...

<html xmlns="http://www.w3/1999/xhtml">
<head>
<title>Get Previous next Day, Month, Year Month Time in Angularjs</title>
<script src="https://ajax.googleapis./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>

发布评论

评论列表(0)

  1. 暂无评论