AngularJS/javascript将日期字符串转换为日期对象
我遇到了一个问题,非常感谢您的帮助。我已经阅读了很多讨论,但它们似乎对我不起作用AngularJS/javascript将日期字符串转换为日期对象,javascript,angularjs,date,Javascript,Angularjs,Date,我遇到了一个问题,非常感谢您的帮助。我已经阅读了很多讨论,但它们似乎对我不起作用 //I have a date as a string which I want to get to a date format of dd/MM/yyyy var collectionDate = '2002-04-26T09:00:00'; //used angularjs date filter to format the date to dd/MM/yyyy collectionDate = $filt
//I have a date as a string which I want to get to a date format of dd/MM/yyyy
var collectionDate = '2002-04-26T09:00:00';
//used angularjs date filter to format the date to dd/MM/yyyy
collectionDate = $filter('date')(collectionDate, 'dd/MM/yyyy'); //This outputs 26/04/2002 as a string
如何将其转换为日期对象?我之所以这样做是因为我想在GoogleCharts指令中使用它,其中一列必须是日期。我不希望列类型为字符串:
例如:
这是我在控制器上做的
var collectionDate = '2002-04-26T09:00:00';
var date = new Date(collectionDate);
//then pushed all my data into an array $scope.rows which I then used in the directive
最后,我将日期格式化为指令中所需的模式,如下所示
var data = new google.visualization.DataTable();
data.addColumn('date', 'Dates');
data.addColumn('number', 'Upper Normal');
data.addColumn('number', 'Result');
data.addColumn('number', 'Lower Normal');
data.addRows(scope.rows);
var formatDate = new google.visualization.DateFormat({pattern: "dd/MM/yyyy"});
formatDate.format(data, 0);
//set options for the line chart
var options = {'hAxis': format: 'dd/MM/yyyy'}
//Instantiate and draw the chart passing in options
var chart = new google.visualization.LineChart($elm[0]);
chart.draw(data, options);
这使我在图表的x轴上看到了dd/MM/yyyy(2002年4月26日)格式的日期。试试这个
html
<div ng-controller="MyCtrl">
Hello, {{newDate | date:'MM/dd/yyyy'}}!
</div>
//JS
//第一种解决方案
时刻(myDate)
//第二种解决方案
时刻(myDate).格式('YYYY-MM-DD HH:MM:ss')
//或
时刻(myDate).格式('YYYY-MM-DD')
//第三种解决方案
myDate=$filter('date')(myDate,“dd/MM/yyyyy”)代码>
{{myDate}日期:'M/d/yyyy HH:mm:ss'}
{{myDate}日期:'medium'}
{{myDate}}
{{myDate}
我知道这是在上面的答案中,但我的观点是,我认为你所需要的是
new Date(collectionDate);
如果您的目标是将日期字符串转换为日期(根据OP“如何将其转换为日期对象?”)
var myApp = angular.module('myApp',[]);
function MyCtrl($scope) {
var collectionDate = '2002-04-26T09:00:00';
$scope.newDate =new Date(collectionDate);
}
new Date(collectionDate);