javascript 如何在Javascript中将日期格式从dd-MMM-yyyy转换为yyyymmdd?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27379243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 07:24:58  来源:igfitidea点击:

How to convert date format from dd-MMM-yyyy to yyyymmdd in Javascript?

javascriptdatedate-formattingdate-conversion

提问by kumareloaded

I've an input text box with a date-picker, when the user selects a particular date, the date say 09-Dec-2014gets applied to the input text box. On submit, I want the date to be passed to the server in yyyymmddformat say 20141209. So how to convert the date format in javascript?

我有一个带有日期选择器的输入文本框,当用户选择特定日期时,日期说09-Dec-2014将应用于输入文本框。提交时,我希望将日期以yyyymmdd格式传递给服务器,例如20141209。那么如何在javascript中转换日期格式呢?

var date = new Date(Date.parse('09-Dec-2014'));

Above code gives me 'Invalid date'.

上面的代码给了我“无效日期”。

Searched the net for solution but was not able to find for my problem.

在网上搜索解决方案,但无法找到我的问题。

Can someone help me out?

有人可以帮我吗?

Thanks.

谢谢。

采纳答案by kumareloaded

Thanks for the answers Danyal Sandeelo and Jeeva Jsb.

感谢 Danyal Sandeelo 和 Jeeva Jsb 的回答。

I was able to get the date in yyyymmddformat using the code given below.

我能够使用下面给出的代码以yyyymmdd格式获取日期。

var date = '09-Dec-2014'.split("-");
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun','Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
for(var j=0;j<months.length;j++){
    if(date[1]==months[j]){
         date[1]=months.indexOf(months[j])+1;
     }                      
} 
if(date[1]<10){
    date[1]='0'+date[1];
}                        
var formattedDate = date[2]+date[1]+date[0];

回答by Danyal Sandeelo

 var date = '09-Dec-2014'.split("-");
 var newDate=date[2]+date[1]+date[0];

Something really quick may be, split and generate your own.

可能真的很快,拆分并生成您自己的东西。

回答by Jeeva Jsb

The problem lies in the format that you pass the required date to the Date() object.

问题在于您将所需日期传递给 Date() 对象的格式。

For some reason browsers surprisingly do not support the date format “yyyy-mm-dd” and therefore fail.

出于某种原因,浏览器出人意料地不支持日期格式“yyyy-mm-dd”,因此失败。

The following formats are definitely supported across all browsers and would advise sticking to one of these to avoid errors:

所有浏览器都绝对支持以下格式,并建议坚持使用其中之一以避免错误:

var d = new Date(2011, 01, 07); // yyyy, mm-1, dd  
var d = new Date(2011, 01, 07, 11, 05, 00); // yyyy, mm-1, dd, hh, mm, ss  
var d = new Date("02/07/2011"); // "mm/dd/yyyy"  
var d = new Date("02/07/2011 11:05:00"); // "mm/dd/yyyy hh:mm:ss"  
var d = new Date(1297076700000); // milliseconds  
var d = new Date("Mon Feb 07 2011 11:05:00 GMT"); // ""Day Mon dd yyyy hh:mm:ss GMT/UTC  

alert(d.getFullYear()+""+(d.getMonth()+1)+""+((d.getDate()<10)?"0"+d.getDate():d.getDate()));

Reference :

参考 :

http://biostall.com/javascript-new-date-returning-nan-in-ie-or-invalid-date-in-safari

http://biostall.com/javascript-new-date-returning-nan-in-ie-or-invalid-date-in-safari