Skip to content Skip to sidebar Skip to footer

Angularjs/javascript Converting A Date String To Date Object

Im stuck on a problem and would appreciate any help. I have read through lot of the discussions already but they dont seem to work for me. //I have a date as a string which I want

Solution 1:

try this

html

<div ng-controller="MyCtrl">
  Hello, {{newDate | date:'MM/dd/yyyy'}}!
</div>

JS

var myApp = angular.module('myApp',[]);

functionMyCtrl($scope) {
    var collectionDate = '2002-04-26T09:00:00'; 

    $scope.newDate =new Date(collectionDate);
}

Demo

Solution 2:

I know this is in the above answers, but my point is that I think all you need is

newDate(collectionDate);

if your goal is to convert a date string into a date (as per the OP "How do I convert it to a date object?").

Solution 3:

This is what I did on the controller

var collectionDate = '2002-04-26T09:00:00';
var date = newDate(collectionDate);
//then pushed all my data into an array $scope.rows which I then used in the directive

I ended up formatting the date to my desired pattern on the directive as follows.

vardata = 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 chartvar options = {'hAxis': format: 'dd/MM/yyyy'}

//Instantiate and draw the chart passing in optionsvar chart = new google.visualization.LineChart($elm[0]);
                    chart.draw(data, options);

This gave me dates ain the format of dd/MM/yyyy (26/04/2002) on the x axis of the chart.

Solution 4:

//JS//First Solutionmoment(myDate)

//Second Solutionmoment(myDate).format('YYYY-MM-DD HH:mm:ss')
//ormoment(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}}

Post a Comment for "Angularjs/javascript Converting A Date String To Date Object"