javascript - 将 c# 日期解析为 javascript 日期如何与 ')' 一起使用?

标签 javascript c#

我知道要将 C# 日期(如 /Date(1430341152570)/)转换为 JavaScript 日期,如下所示:

var part = "/Date(1430341152570)/".substr(6); // => "1430341152570)/"
var jsDate = new Date(parseInt(part));

我的问题:如何通过 parseInt 将包含尾随 )/part 值解析为 int? JS 不会在尝试转换具有 )/ 字符的内容时抛出错误吗?如果 part 值类似于 "/Date(......)/".substr(6).replace(')/',对我来说会更有意义, '') b/c 至少你正在将它变成一个要解析为 int 的字符串。

最佳答案

来自Mozilla documentation :

If parseInt encounters a character that is not a numeral in the specified radix, it ignores it and all succeeding characters and returns the integer value parsed up to that point. parseInt truncates numbers to integer values. Leading and trailing spaces are allowed.

) 不是数字,因此按照指定从那里开始的所有内容都将被忽略。

或来自ES6 draft standard :

parseInt may interpret only a leading portion of string as an integer value; it ignores any code units that cannot be interpreted as part of the notation of an integer, and no indication is given that any such code units were ignored.

关于javascript - 将 c# 日期解析为 javascript 日期如何与 ')' 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29967599/

相关文章:

javascript - 如何在 ES6 中使用 React 编写交互式 d3 组件?

c# - WebForms 本地化和资源文件实际实现

具有 SecuritySafeCritical 函数的 C# 完全信任的程序集仍然抛出安全异常

javascript - cheerio 脚本元素 - 数据

javascript - 当我从指令更新时,$scope.$watch 不会触发

c# - 获取 Entity Framework 中每个组的最后记录?

c# - 使用指令导入子命名空间

c# - 使用 WinRT 检索设备的序列号

javascript - 如何在 Javascript 中使用 Q 顺序运行 promise ?

javascript - 端到端 javascript 堆栈安全吗?