AngularJS / javascript中的日期字符串转换为日期对象 [英] AngularJS/javascript converting a date String to date object

查看:507
本文介绍了AngularJS / javascript中的日期字符串转换为日期对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

林停留在一个问题,也会AP preciate任何帮助。我已经通过很多的讨论已经看过,但他们不似乎为我工作。

  //我有一个日期作为字符串,我想要得到的dd日期格式/ MM / YYYY
变种collectionDate ='2002-04-26T09:00:00';//使用angularjs日期过滤器来格式化日期DD / MM / YYYY
collectionDate = $过滤器('日期')(collectionDate,DD / MM / YYYY'); //这个输出26/04/2002作为一个字符串

我如何将其转换为日期对象?我想这样做的原因是因为我想在谷歌图表指令,其中一列已是日期使用它。我不希望有列类型为字符串:

例如:

  VAR数据=新google.visualization.DataTable();
                    data.addColumn('日','日期');
                    data.addColumn('号','正常值');
                    data.addColumn('号','结果');
                    data.addColumn('号','低于正常');
                    data.addRows(scope.rows); .................


解决方案

这是我做过什么控制器上

  VAR collectionDate ='2002-04-26T09:00:00';
VAR日期=新的日期(collectionDate);
//然后我的所有数据推入一个数组$ scope.rows我然后在指令中使用

我结束了在格式化指令的日期到我期望的图案如下。

  VAR数据=新google.visualization.DataTable();
                    data.addColumn('日','日期');
                    data.addColumn('号','正常值');
                    data.addColumn('号','结果');
                    data.addColumn('号','低于正常');
                    data.addRows(scope.rows);
                    VAR formatDate =新google.visualization.DateFormat({模式:DD / MM / YYYY});
                    formatDate.format(数据,0);
//为线图设置选项
VAR的选择= {'hAxis':格式:'DD / MM / YYYY}//实例化并绘制图表传递选项
变种图表=新google.visualization.LineChart($榆树[0]);
                    chart.draw(数据,选项);

这给了我艾因日期DD的格式/ MM / YYYY(26/04/2002)在图表的x轴。

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

How do I convert it to a date object? The reason I want to do this is because I want to use it in a google charts directive where one of the columns has to be a date. I do not want to have the column type as string:

eg:

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

解决方案

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.

这篇关于AngularJS / javascript中的日期字符串转换为日期对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆