Click here to Skip to main content
15,917,060 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I need to change the date formats as ,if I enter the date as mm/dd/yyyy i need that to be changed to dd/mm/yyyy or if i enter the date as dd/mm/yyyy i need to change to m mm/dd/yyyy.
It should detect the entry and change accordingly,is it possible? how can i do it..anybody please help..
The below code is my try..
C#
var dateformat=row.date_format;
                          if(dateformat=="dd/mm/yyyy")                    //checking fot the date format//
                          {
                   var value = document.getElementById('pay_date').value;
                           var today = new Date(value);
                           if((today.getMonth() + 1)<10)
                           {
                             mm='0'+(today.getMonth() + 1)
                           }
                           else
                           {
                             mm=(today.getMonth() + 1)
                           }
                           if(today.getDate()<10)
                           {
                             dd='0'+today.getDate()
                           }
                           else
                           {
                             dd=today.getDate()
                           }
                           var yyyy = today.getFullYear();
                       document.getElementById('date').value=dd + '/' + mm + '/' + yyyy;
                          }
                        else
                         {
                      myDate = new Date(document.getElementById('pay_date').value);
                          var day = myDate.getDate();
                          var month = myDate.getMonth() + 1;
                          var yr = myDate.getFullYear();
                      document.getElementById('pay_date').value=month+"/"+day+ "/" +yr;
                         }

According to above,if the dateformat is dd/mm/yyyy(set before)then if i enter in the textfield in the mm/dd/yyyy format,then it should change..hope its clear..
The above code works when I type date as 12/03/2011 it changes to 03/12/2011.
but if I write 13/12/2011,it shows NAN/NAN/NAN..What is wrong?
Posted
Comments
Mano N 28-Jun-14 8:56am    
Debug the code and see waht error is actually happened
Mohibur Rashid 30-Jun-14 5:12am    
My suggestion is don't let the user type into text box.
make it readonly. You will get lots of plugin in google search. Use one of them

C#
function Dateformat(date) {


var d = new Date(parseInt(date.substr(6)));
var m, day;
m = d.getMonth() + 1;
if (m < 10)
m = '0' + m
if (d.getDate() < 10)
day = '0' + d.getDate()
else
day = d.getDate();
var formattedDate = m + "/" + day + "/" + d.getFullYear();
var hours = (d.getHours() < 10) ? "0" + d.getHours() : d.getHours();
var minutes = (d.getMinutes() < 10) ? "0" + d.getMinutes() : d.getMinutes();
var formattedTime = hours + ":" + minutes + ":" + d.getSeconds();
formattedDate = formattedDate + " " + formattedTime;
return formattedDate;
}
 
Share this answer
 
C#
DateTime dtm = new DateTime(2014, 05, 15);
String val = dtm.ToString("dd-mm-yyyy");//Result :15-05-2014
String val = dtm.ToString("dd/mm/yyyy");//Result :15/05/2014
String val = dtm.ToString("dd*mm*yyyy");//Result :15*05*2014
String val = dtm.ToString("mm-dd-yyyy");//Result :05-15-2014
String val = dtm.ToString("dd-yyyy-mm");//Result :15-2014-05
String val = dtm.ToString("yyyy!dd!mm");//Result :2014!15!05
.
.
.
.
.
//you can modify in every arrange and character spliter
 
Share this answer
 
v3

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