반응형
AngularJS/Javascript 날짜 문자열에서 날짜 개체로 변환
나는 어떤 문제에 매달렸고 어떤 도움이라도 주었으면 한다.나는 이미 많은 토론들을 읽었지만 그것들은 나에게 효과가 없는 것 같다.
//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
날짜 개체로 변환하려면 어떻게 해야 합니까?이 작업을 수행하는 이유는 구글 차트 지침에서 열 중 하나가 날짜여야 하기 때문입니다.열 유형을 문자열로 지정하지 않습니다.
예:
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);
}
위의 답변에 있는 건 알지만, 제 요점은 당신이 필요한 건
new Date(collectionDate);
날짜 문자열을 날짜로 변환하는 것이 목표인 경우(OP "How do I convert it to date object?"에 따라).
이게 제가 컨트롤러에서 한 일입니다.
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/yyy (2002년 4월 26일)가 되었습니다.
//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}}
언급URL : https://stackoverflow.com/questions/25027813/angularjs-javascript-converting-a-date-string-to-date-object
반응형
'programing' 카테고리의 다른 글
Jquery 및 HTML FormData는 "Uncatched TypeError:부정한 호출" (0) | 2023.03.10 |
---|---|
Bash 변수를 사용하여 JSON 문자열 구축 (0) | 2023.03.10 |
스크립트 실행을 거부했습니다. 엄격한 MIME 유형 검사가 사용되도록 설정되었습니까? (0) | 2023.03.10 |
cPanel에서 최대 업로드 크기를 늘리는 방법 (0) | 2023.03.10 |
위험하게 SetInner를 대체하는 안전한 방법HTML (0) | 2023.03.10 |