如何更改日期格式? [英] How to change the date formats?

查看:81
本文介绍了如何更改日期格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要更改日期格式,如果我输入日期为mm / dd / yyyy我需要将其更改为dd / mm / yyyy或者如果我输入日期为dd / mm / yyyy我需要改为m mm / dd / yyyy。

它应该检测输入并相应地改变,是否可能?我怎么能这样做..有人请帮忙..

下面的代码是我的尝试..

  var  dateformat = row.date_format; 
if (dateformat == dd / mm / yyyy // 检查日期格式//
{
var value = document.getElementById( ' pay_date')。 value ;
var today = new 日期( value < /跨度>);
if ((today.getMonth()+ 1 )< 10)
{
mm = ' 0' +(today.getMonth()+ 1
}
else
{
mm =(今天.getMonth()+ 1
}
if (today.getDate( )< 10)
{
dd = ' 0' +今天。 getDate()
}
else
{
dd = today.getDate()
}
var yyyy = today.getFullYear();
document.getElementById(' date')。 value = dd + ' /' + mm + ' /' + yyyy;
}
其他
{
myDate = new 日期(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;
}



根据以上所述,如果dateformat是dd / mm / yyyy(之前设置),那么如果我输入mm / dd /中的textfield yyyy格式,然后它应该改变..希望它明确..

以上代码适用于我输入日期为12/03/2011它更改为03/12/2011。

但如果我写了13/12/2011,它会显示NAN / NAN / NAN ..出了什么问题?

解决方案

 function Dateformat(date){


var d = new 日期(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;
}


 DateTime dtm =  new  DateTime ( 2014  05  15 ); 
字符串 val = dtm.ToString( dd-mm-yyyy); // 结果:1​​5-05-2014
字符串 val = dtm.ToString( dd / mm / yyyy); // 结果:1​​5/05/2014
字符串 val = dtm.ToString( dd * mm * yyyy); // 结果:1​​5 * 05 * 2014
字符串 val = dtm.ToString( mm-dd -yyyy); // 结果:05-15-2014
String val = dtm.ToString( dd-yyyy-mm); // 结果:1​​5-2014-05
字符串 val = dtm.ToString( yyyy!dd!mm); // 结果:2014!15!05





// 您可以在每个排列和字符拆分器中进行修改


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..

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?

解决方案

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;
}


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


这篇关于如何更改日期格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆