调用 javascript 时间“对象”
我的一个 Web 服务返回一个以下格式的对象。
{"UserID":"338040a3-6587-42bf-b22e-dc88f4ea3a9c","CompanyID":"823946a4-29d0-4546-a7ca-790648cf1e1a","FirstName":"Maxim","LastName":"Gershkovich","Username":"MaximG","Password":"62198e58d57843967bc34824be77ee2f1e2abcbbe389dc41c06810ed6f9561bf","Email":"[email protected]","SignupDate":"/Date(1272631625210)/","IsAdmin":true,"LoginTime":"/Date(1297129238517)/"}"
获取它返回的日期时间值并将其转换为 JavaScript 日期对象的最佳方法是什么?
"SignupDate":"/Date(1272631625210)/"
我能想到的唯一方法是删除第一个和最后一个字符,然后进行评估。有更好的办法吗?
A webservice of mine returns an object in the following format.
{"UserID":"338040a3-6587-42bf-b22e-dc88f4ea3a9c","CompanyID":"823946a4-29d0-4546-a7ca-790648cf1e1a","FirstName":"Maxim","LastName":"Gershkovich","Username":"MaximG","Password":"62198e58d57843967bc34824be77ee2f1e2abcbbe389dc41c06810ed6f9561bf","Email":"[email protected]","SignupDate":"/Date(1272631625210)/","IsAdmin":true,"LoginTime":"/Date(1297129238517)/"}"
What is the optimal way to take the datetime value it returns and convert it to a javascript date object?
"SignupDate":"/Date(1272631625210)/"
The only way I can think of is to strip the first and last char and then do an eval. Is there a better way?
您可以使用正则表达式和日期构造函数来完成此操作。
不确定是否更好,但更安全。 Eval 有时可能是邪恶的......
所以,你可以这样做:
你也可以用替换来执行正则表达式:
You can do it with regex and Date constructor.
Not sure if it better, but it is safer. Eval can be evil sometimes...
So, you could do something like this:
You could also do the regex with replace: