問題に行き詰まっているので、助けていただけると助かります。すでに多くの議論に目を通しましたが、私にはうまくいかないようです。
//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
これを日付オブジェクトに変換するにはどうすればいいですか?これを行いたい理由は、列の1つが日付でなければならないgoogle chartディレクティブで使用したいからです。列のタイプをstringにはしたくありません。
例えば
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);.................
試しに
html。
<div ng-controller="MyCtrl">
Hello, {{newDate | date:'MM/dd/yyyy'}}!
</div>
JS
var myApp = angular.module('myApp',[]);
function MyCtrl($scope) {
var collectionDate = '2002-04-26T09:00:00';
$scope.newDate =new Date(collectionDate);
}
[デモ][1]
コントローラーにはこんなことをしました
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(26/04/2002)という形式の日付が表示されました。
//JS
//First Solution
moment(myDate)
//Second Solution
moment(myDate).format('YYYY-MM-DD HH:mm:ss')
//or
moment(myDate).format('YYYY-MM-DD')
//Third Solution
myDate = $filter('date')(myDate, "dd/MM/yyyy");
<!--HTML-->
<!-- First Solution -->
{{myDate | date:'M/d/yyyy HH:mm:ss'}}
<!-- or -->
{{myDate | date:'medium'}}
<!-- Second Solution -->
{{myDate}}
<!-- Third Solution -->
{{myDate}}