简体   繁体   English

将Java的DateTime对象转换为js中的字符串

[英]Convert DateTime object of java to string in js

I m developing a java application. 我正在开发Java应用程序。 In this I am using Restangular service , angular js and spring frame work. 在此,我使用Restangular服务,angular js和spring框架工作。 I am passing DateTime Object from restangular call to js. 我正在从Restangular调用中将DateTime对象传递给js。 My restangular code is - 我的矩形代码是-

@GET
@Path("/try")
@Timed
public DateTime create() {
    return new DateTime("2016-05-30T12:10:30.407+05:30");
}

And in js I want this object as string. 在js中,我希望此对象为字符串。 My js code is - 我的js代码是-

$scope.try = function () {
    console.log("Hi")
    Restangular.one('tasks/try').get().then(function (data) {
        console.log(data);
    });
};

In this code the returned restangular object is in form 2016-05-30T12:10:30.407+05:30 but in JS I recieve it as [2016, 6, 2, 16, 55, 52, 931] What can I do so that in JS I get this datetime object as 2016-05-30T12:10:30.407+05:30 ? 在此代码中,返回的矩形对象的格式为2016-05-30T12:10:30.407+05:30但是在JS中,我将其接收为[2016, 6, 2, 16, 55, 52, 931] 2016-05-30T12:10:30.407+05:30 [2016, 6, 2, 16, 55, 52, 931]在JS中,我将此日期时间对象作为2016-05-30T12:10:30.407+05:30

Thanks in advance. 提前致谢。

You can use 您可以使用

data = [2016, 6, 2, 16, 55, 52, 931];

var thisDate = new (Function.prototype.bind.apply(Date, [null].concat(data)));

Here we are basically passing Date function with all the elements in your data array programmatically. 在这里,我们基本上通过编程方式将Date函数与数据数组中的所有元素一起传递。

ALTER 改变

this method will be a bit long but will give you a timestamp by which you can convert to any formate 该方法会有点长,但是会给您一个时间戳,您可以通过该时间戳转换为任何形式

function convertToTimeStamp (array) {
    var str = array[0] + "," + array[1] + "," + array[2];
    _ts = new Date(str).getTime()
    timeStamp = _ts + (array[3]*3600 + array[4]*60 + array[5])*1000 + array[6];
    return timeStamp;
}

now you can call convertToTimeStamp(array) to get a timeStamp value 现在您可以调用convertToTimeStamp(array)以获取timeStamp值

you can convert the timeStamp to any format by converting it to a date Object new Date(timeStamp) 您可以通过将timeStamp转换为日期将其转换为任何格式对象new Date(timeStamp)

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

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