[英]Javascript convert date to different format
I have a date picker which is in the format of 31-Dec-2014
as this is the format everyone is familiar with on our tool. 我有一个日期选择器,其格式为31-Dec-2014
因为这是我们工具上每个人都熟悉的格式。
I am trying to convert it to a JavaScript date object in the format of mm/dd/yyyy
but I am having a little trouble. 我正在尝试将其转换为mm/dd/yyyy
格式的JavaScript日期对象,但遇到了一些麻烦。
I tried something simple like var startDate = new Date('31-Dec-2014');
我尝试了一些简单的操作,例如var startDate = new Date('31-Dec-2014');
but it outputs invalid date. 但它输出无效的日期。
I know there are plugins out there like date.js
and moment.js
but I feel like this would be simple to do without it. 我知道那里有类似date.js
和moment.js
插件,但是我觉得如果没有它,这将很容易。
Any ideas? 有任何想法吗?
Use the 3 param version of Date and translate your month code to the correct number, eg 使用Date的3参数版本并将您的月份代码转换为正确的数字,例如
function myDate(input) {
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
input = input.split('-');
input[1] = months.indexOf(input[1]);
return new Date(+input[2], +input[1], +input[0]);
}
myDate('31-Dec-2014'); // Wed Dec 31 2014 00:00:00 GMT+0000 (GMT Standard Time)
Please remember all dates are generated in the client's local timezone, you may want to use the UTC methods instead. 请记住,所有日期都是在客户的本地时区中生成的,您可能想改用UTC方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.