📜  jquery 日期格式 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:02:43.284000             🧑  作者: Mango

代码示例6
GetDatePattern("MM/dd/yyyy");
 function GetDatePattern(pattern)
    {
    var monthNames=["January", "February", "March", "April", "May", "June",
                "July", "August", "September", "October", "November", "December"];
    
            var todayDate = new Date();
                                              
                            var date = todayDate.getDate().toString();
                            var month = todayDate.getMonth().toString(); 
                            var year = todayDate.getFullYear().toString(); 
                            var formattedMonth = (todayDate.getMonth() < 10) ? "0" + month : month;
                            var formattedDay = (todayDate.getDate() < 10) ? "0" + date : date;
                            var result = "";
    
                            switch (pattern) {
                                case "M/d/yyyy": 
                                    formattedMonth = formattedMonth.indexOf("0") == 0 ? formattedMonth.substring(1, 2) : formattedMonth;
                                    formattedDay = formattedDay.indexOf("0") == 0 ? formattedDay.substring(1, 2) : formattedDay;
    
                                    result  = formattedMonth + '/' + formattedDay + '/' + year;
                                    break;
    
                                case "M/d/yy": 
                                    formattedMonth = formattedMonth.indexOf("0") == 0 ? formattedMonth.substring(1, 2) : formattedMonth;
                                    formattedDay = formattedDay.indexOf("0") == 0 ? formattedDay.substring(1, 2) : formattedDay;
                                    result  = formattedMonth + '/' + formattedDay + '/' + year.substr(2);
                                    break;
    
                                case "MM/dd/yy":
                                    result  = formattedMonth + '/' + formattedDay + '/' + year.substr(2);
                                    break;
    
                                case "MM/dd/yyyy":
                                   result  = formattedMonth + '/' + formattedDay + '/' + year;
                                    break;
    
                                case "yy/MM/dd":
                                    result  = year.substr(2) + '/' + formattedMonth + '/' + formattedDay;
                                    break;
    
    
                                case "yyyy-MM-dd":
                                    result  = year + '-' + formattedMonth + '-' + formattedDay;
                                    break;
    
                                case "dd-MMM-yy":
                                   result  = formattedDay + '-' + monthNames[todayDate.getMonth()].substr(3) + '-' + year.substr(2);
                                    break;
    
                                case "MMMM d, yyyy":
                                    result  = todayDate.toLocaleDateString("en-us", { day: 'numeric', month: 'long', year: 'numeric' });
                                    break;
    
    
                            }
                            }