简体   繁体   English

Angularjs 和 Google Charts 将日期转换为 JSON 日期?

[英]Angularjs and Google Charts convert Date in JSON Date?

i want build on server side (spring/java) a DataTable or DataArray that i can use for google charts.我想在服务器端(spring/java)上构建一个可用于谷歌图表的 DataTable 或 DataArray。 The Problem is the Date Format.问题是日期格式。 I have first build a Data Array and serialized it to json that looks like that:我首先构建了一个数据数组并将其序列化为如下所示的 json:

{DataArray:[["Date","New York","Austin"],[100,200,300]]} {DataArray:[["Date","New York","Austin"],[100,200,300]]}

on the client side i can parse this DataArray to a DataTable this is no problem..在客户端,我可以将此 DataArray 解析为 DataTable,这没问题..

But if i want add a Date, Angular throws an error because the json string is then not well formated.但是如果我想添加一个日期,Angular 会抛出一个错误,因为 json 字符串的格式不正确。

on the client side i can use this, this creates me from an array a DataTable:在客户端,我可以使用它,这会从数组中创建一个 DataTable:

data = new google.visualization.arrayToDataTable([ ['date','New York','Austin','San Francisco','Country','Popularity'], [new Date("8/6/1975"),2,3,15,'Germany',200], [new Date("8/6/1980"),5,6,19,'Brazil',400], [new Date("8/6/2000"),8,9,23,'United States',300], [new Date("8/6/2010"),11,12,35,'RU',700] ]) data = new google.visualization.arrayToDataTable([ ['date','New York','Austin','San Francisco','Country','Popularity'], [new Date("8/6/1975") ,2,3,15,'德国',200], [新日期("8/6/1980"),5,6,19,'巴西',400], [新日期("8/6/2000") "),8,9,23,'美国',300], [新日期("8/6/2010"),11,12,35,'RU',700]])

there i can use new Date() this worked but how can i create for example "new Date("8/6/1975")" on server side and send it to my client and then on client side it should create a Date Object.在那里我可以使用 new Date() 这有效但是我如何在服务器端创建例如“new Date("8/6/1975")”并将其发送到我的客户端,然后在客户端它应该创建一个日期对象.

Has someone an idear how i can do that ?有人知道我该怎么做吗? Can i use the json date format ?我可以使用 json 日期格式吗? If so, how can i create on server side a json date that looks on the client side like that "new Date("8/6/1975")" and creates me an Date object ?如果是这样,我如何在服务器端创建一个在客户端看起来像“新日期(“8/6/1975”)”的json日期并为我创建一个日期对象?

You need to specify your dates as strings on the server-side, in this format:您需要在服务器端将日期指定为字符串,格式如下:

'Date(year, month, day, hour, minute, second, millisecond)'

where month is 0-indexed (so January is 0, not 1), and month , day , hour , minute , second , and millisecond are all optional (default is 1 for day and 0 for all others).其中month是 0 索引(所以一月是 0,而不是 1),并且monthdayhourminutesecondmillisecond都是可选的( day默认值为1 ,所有其他的默认值为0 )。

Your header for the date column also needs to specify that the column is a "date" type, otherwise the arrayToDataTable method will interpret it as a string.日期列的标题还需要指定该列是“日期”类型,否则arrayToDataTable方法会将其解释为字符串。 Instead of using the string 'date' in the header row, use an object:不要在标题行中使用字符串'date' ,而是使用一个对象:

{label: 'date', type: 'date'}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM