javascript - 如何设置 toLocaleString 的通用格式?

标签 javascript angularjs

我使用 JS 函数 toLocaleString 进行日期格式化。如何为所有客户端设置一种通用格式,例如:

2015-10-29 20:00:00

我通过 -

在 PHP 上做 parsong

最佳答案

我认为您必须手动将其解析为该格式,这实际上还算不错。 Date.toLocaleString() 返回的格式为:

MM/DD/YYYY, HH:MM:SS

这是我的代码片段,可以帮助您解决问题:

// Parse our locale string to [date, time]
var date = new Date().toLocaleString('en-US',{hour12:false}).split(" ");

// Now we can access our time at date[1], and monthdayyear @ date[0]
var time = date[1];
var mdy = date[0];

// We then parse  the mdy into parts
mdy = mdy.split('/');
var month = parseInt(mdy[0]);
var day = parseInt(mdy[1]);
var year = parseInt(mdy[2]);

// Putting it all together
var formattedDate = year + '-' + month + '-' + day + ' ' + time;

关于javascript - 如何设置 toLocaleString 的通用格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33057291/

相关文章:

javascript - 如何将 NodeJS 包与 RequireJS 一起使用?

java - Struts2 - 在 JSP 迭代期间调用 Javascript 函数

javascript - { [错误 : failed [405] HTTP method not allowed, 支持的方法:POST] 堆栈:[Getter] }

javascript - 使用 angularjs 的 facebook 页面图评论的顺序

asp.net-mvc - 可以在 Angular 中动态加载模块吗?

javascript - 问题从 javascript 调用 jquery 函数

javascript - Firefox jquery position() 关闭 1px

javascript - 如何以 Angular 检查离线和在线

javascript - document.location 尝试下载文件时修改 WebApi 路由

javascript - 我正在尝试使用 Angular js 从输入中调用文本