AngularJS/javascript converting a date String to date object

后端 未结 4 1111
终归单人心
终归单人心 2020-12-09 15:56

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 a         


        
相关标签:
4条回答
  • 2020-12-09 16:19

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

    new Date(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?").

    0 讨论(0)
  • 2020-12-09 16:23

    This is what I did on the controller

    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
    

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

    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);
    

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

    0 讨论(0)
  • 2020-12-09 16:25

    //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}}

    0 讨论(0)
  • 2020-12-09 16:33

    try this

    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);
    }
    

    Demo

    0 讨论(0)
提交回复
热议问题