Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
i have a three variables.. one have a date value.. from_format in one variable and to_format in another variable.. i want to convert date from from_format into to_format.. How its can be done??

Ex :
var date_value ="05/08/2015/";
var from_format = "dd/MM/yyyy";
var to_format = "yyyy/MM/dd";
Posted
Comments
Kornfeld Eliyahu Peter 13-Jul-15 9:27am    
SnvMohan 13-Jul-15 23:48pm    
i google this but nothing to find out.. always i have a from format and to format and value... i want to convert fromformat into toformat..

Hello,

It Returns the Date in Format of yyyy-MM-dd

function formatDate(date) {
     var d = new Date(date),
         month = '' + (d.getMonth() + 1),
         day = '' + d.getDate(),
         year = d.getFullYear();

     if (month.length < 2) month = '0' + month;
     if (day.length < 2) day = '0' + day;

     return [year, month, day].join('-');
 }
 alert(formatDate('05/08/2015'));
 
Share this answer
 
var DateCreated = new Date(Date.parse(Your Date Here)).format("MM/dd/yyyy");
 
Share this answer
 
JavaScript
function(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;


                        }
						}
 
Share this answer
 
Comments
CHill60 23-Mar-21 8:43am    
Incomplete. Only works with today's date instead of date value

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900