更改日期格式以确定日期和月份? [英] Change the date format my identifying the date and month?

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

问题描述

  function date()
{
var myDate = new Date(document.getElementById('date')。value); //从textfield获取日期
var day = myDate.getDate();
var month = myDate.getMonth()+ 1;
var yr = myDate.getFullYear();

if(dateformat ==dd / mm / yyyy)//检查日期格式//
{

document.getElementById('date') .value = day +/+ month +/+ yr;
}
else
{

document.getElementById('date')。value = month +/+ day +/+ yr;




$ b $ p
$ b

这个函数onchange是用文本框日期的onchange mehod ,在更改文本字段时,它应该更改默认设置的日期格式。



如果 dd / mm / yyy 是格式,然后以该格式进行更改,如果 mm / dd / yyy 则以此格式进行更改。我的代码执行了更改,但无法识别哪个月例如.. 如果日期格式为 mm / dd / yyy $ b

我输入日期为 11/01/2001'(2001年11月),它改变为 01/11/2001,这是不应该做的。
但如果我输入01-11-2001(1月1日2001年),输入的内容是正确的,但它改变为11/01/2001



我改变了代码来纠正这个? plz help !!!

解决方案

Demo Fiddle
$ b $ em

  function dateChange(){
var e = document.getElementById(dateformat);
var dateformat = e.options [e.selectedIndex] .text;
var myDate;
if(dateformat ==dd / mm / yyyy)//检查日期格式//
{
var value = document.getElementById('date')。value;
var format = value.split(/);
myDate = new Date(format [2],format [1] - 1,format [0]);
var day = myDate.getDate();
var month = myDate.getMonth()+ 1;
var yr = myDate.getFullYear();
document.getElementById('date')。value = day +/+ month +/+ yr;
} else {
myDate = new Date(document.getElementById('date').value);
var day = myDate.getDate();
var month = myDate.getMonth()+ 1;
var yr = myDate.getFullYear();
document.getElementById('date')。value = month +/+ day +/+ yr;
}
document.getElementById('dateStr')。innerHTML = myDate.toDateString();
}

输入日期 01/02/2014 mm / dd / yyyy 下拉式日期将为 2014年1月2日 ,现在只需将下拉列表更改为 dd / mm / yyyy 日期为 2014年2月01日 code
$ b

实例化Javascript的Date对象需要这种格式 new Date(yyyy,mm,dd),所以当你使用 dd / mm / yyyy 时,你需要手动交换 dd& mm values ...

参考


function date()
{
   var myDate=new Date(document.getElementById('date').value); //get the date from a textfield
var day=myDate.getDate();
    var month=myDate.getMonth()+1;
var yr=myDate.getFullYear();

      if(dateformat=="dd/mm/yyyy")         //checking the date format //
      {

        document.getElementById('date').value=day + "/" + month + "/" + yr;
      }
    else
     { 

     document.getElementById('date').value=month+"/"+day + "/" +yr;
     }
   }

This function onchange is written in onchange mehod of textbox date,on changing the textfield it should change the date format that is set by default.

If dd/mm/yyy is the format then change it in that format and if mm/dd/yyy then change in this format.My code does the changes, but it cannot recognize which is the month!

For example.. if the date format is mm/dd/yyy and I type the date as 11/01/2001' (NOV -1 2001) it changes to01/11/2011` which should not be done. But if I type 01-11-2001 (jan 1 2001) which is entered is correct ,but it changes to 11/01/2001

How can I change the code to correct this??? plz help!!!

解决方案

Demo Fiddle
Javascript Code

function dateChange() {
    var e = document.getElementById("dateformat");
    var dateformat = e.options[e.selectedIndex].text;
    var myDate;
    if (dateformat == "dd/mm/yyyy") //checking the date format //
    {
        var value = document.getElementById('date').value;
        var format = value.split("/");
        myDate = new Date(format[2], format[1] - 1, format[0]);
        var day = myDate.getDate();
        var month = myDate.getMonth() + 1;
        var yr = myDate.getFullYear();
        document.getElementById('date').value = day + "/" + month + "/" + yr;
    } else {
        myDate = new Date(document.getElementById('date').value);
        var day = myDate.getDate();
        var month = myDate.getMonth() + 1;
        var yr = myDate.getFullYear();
        document.getElementById('date').value = month + "/" + day + "/" + yr;
    }
    document.getElementById('dateStr').innerHTML = myDate.toDateString();
}  

Enter the date 01/02/2014 with mm/dd/yyyy in drop down the date would be Thu Jan 02 2014, now just change the drop down to dd/mm/yyyy the date would be Sat Feb 01 2014

Instantiating Javascript's Date object require this format new Date(yyyy,mm,dd), so when you use dd/mm/yyyy you need to manually ex-change the dd & mm values...
Reference

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

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