100分求一个js处理日期格式的函数,具体要求请进

enxi 2006-12-27 03:47:44
js中有一个变量date,接收到的值格式不一,目前有如下几种情况:
2006-11-28 10:43:52
Wed, 13 Dec 2006 09:31:36 GMT+8
Wed, 13 Dec 2006 18:25:22 +0800
...还有其它日期格式

我现在要求有一个这样的通用日期处理函数,将它格式化为“月-日”形式,如:11-28
...全文
1093 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
enxi 2006-12-27
  • 打赏
  • 举报
回复
ice_berg16(寻梦的稻草人) 太猛了,你写的正是我所要,谢啦。
ice_berg16 2006-12-27
  • 打赏
  • 举报
回复
<script>
var date = ["2006-11-28 10:43:52","Wed, 13 Dec 2006 09:31:36 GMT+8","Wed, 13 Dec 2006 18:25:22 +0800"];
for(i=0;i<date.length;i++)
{
alert(parseDate(date[i]));
}

function parseDate(format){
if(/\d{4}-\d{2}-\d{2}/.test(format))
format = format.replace(/(\d{4})-(\d{2})-(\d{2})/, "$2/$3/$1");
var t = Date.parse(format);
if(isNaN(t)) alert('不可识别的格式');
var d = new Date(t);
return (d.getMonth()+1+"月"+d.getDate()+"日");
}
</script>
circlew20 2006-12-27
  • 打赏
  • 举报
回复
function MM_dateFormat(rq,fenge) {
//格式化日期显示
//fenge = 0 :2006-02-28 12:34:23
//fenge = 1 :2006-02-28
//fenge = 2 :2006年2月28日
//fenge = 3 :2月28日
//fenge = 4 :28/2
//fenge = 5 :二○○六年二月二十八日
//fenge = 6 :12:34:23
if ((fenge==null)||(fenge=="")) fenge = 0;
if ((rq =="")||(rq ==null)) var tmpDate = new Date(); else var tmpDate = new Date(rq);
var result = "";
switch(fenge) {
case 1:
result += tmpDate.getFullYear()+"-";
if ((tmpDate.getMonth()+1)10) result += "0"+(tmpDate.getMonth()+1)+"-"; else result += (tmpDate.getMonth()+1)+"-";
if (tmpDate.getDate()<10) result += "0"+tmpDate.getDate(); else result += tmpDate.getDate();
break;
case 2:
result = tmpDate.getFullYear()+"年"+(tmpDate.getMonth()+1)+"月"+tmpDate.getDate()+"日";
break;
case 3:
result = (tmpDate.getMonth()+1)+"月"+tmpDate.getDate()+"日";
break;
case 4:
result = tmpDate.getDate()+"/"+(tmpDate.getMonth()+1);
break;
case 5:
var zhifu= "○一二三四五六七八九十";
var zhrq ="";
var nan = String(tmpDate.getFullYear());
for (iii=0;iii<nan.length;iii++) { zhrq += zhifu.substr(nan.substr(iii,1),1); }
zhrq += "年";
var nan = String(tmpDate.getMonth()+1);
if (nan.length == 2) {
if (nan.substr(0,1)=="1") zhrq += "十"; else zhrq += zhifu.substr(nan.substr(0,1),1)+"十";
if (nan.substr(1,1)=="0") zhrq += "月"; else zhrq += zhifu.substr(nan.substr(1,1),1)+"月"
} else {
zhrq += zhifu.substr(nan.substr(0,1),1)+"月";
}
var nan = String(tmpDate.getDate());
if (nan.length == 2) {
if (nan.substr(0,1)=="1") zhrq += "十"; else zhrq += zhifu.substr(nan.substr(0,1),1)+"十";
if (nan.substr(1,1)=="0") zhrq += "日"; else zhrq += zhifu.substr(nan.substr(1,1),1)+"日"
} else {
zhrq += zhifu.substr(nan.substr(0,1),1)+"日";
}
result = zhrq;
break;
case 6:
if (tmpDate.getHours()10) result += "0"+tmpDate.getHours(); else result += ""+ tmpDate.getHours();
if (tmpDate.getMinutes()10) result += ":0"+tmpDate.getMinutes(); else result += ":"+ tmpDate.getMinutes();
if (tmpDate.getSeconds()<10) result += ":0"+tmpDate.getSeconds(); else result += ":"+ tmpDate.getSeconds();
break;
default:
result += tmpDate.getFullYear()+"-";
if ((tmpDate.getMonth()+1)<10) result += "0"+(tmpDate.getMonth()+1)+"-"; else result += (tmpDate.getMonth()+1)+"-";
if (tmpDate.getDate()<10) result += "0"+tmpDate.getDate(); else result += tmpDate.getDate();
if (tmpDate.getHours()<10) result += " 0"+tmpDate.getHours(); else result += " "+ tmpDate.getHours();
if (tmpDate.getMinutes()<10) result += ":0"+tmpDate.getMinutes(); else result += ":"+ tmpDate.getMinutes();
if (tmpDate.getSeconds()<10) result += ":0"+tmpDate.getSeconds(); else result += ":"+ tmpDate.getSeconds();
break;
}
return result
}


你想怎么样的日期 自己选 嘿嘿
enxi 2006-12-27
  • 打赏
  • 举报
回复
都没能解决我的问题啊
懒得去死 2006-12-27
  • 打赏
  • 举报
回复
function specialText_DateAdd(oInput,isMinus)
{
var arrValue = oInput.value.split(oInput.middleChar);
var strYear = arrValue[0];
var strMonth = arrValue[1];
var strDate = arrValue[2];
var monthMaxDates = specialText_getMonthDates(strYear,strMonth);

//true
if(isMinus)
{
arrValue[2] = parseInt(strDate,10) - 1;
if(parseInt(arrValue[2],10) == 0)
arrValue[2] = monthMaxDates;
}
else
{
arrValue[2] = parseInt(strDate,10) + 1;
if(parseInt(arrValue[2],10) == (monthMaxDates + 1))
arrValue[2] = "01";
}
oInput.value = arrValue.join(oInput.middleChar);
specialText_validDate(oInput);
specialText_SelectDate(oInput);
}

function specialText_KeyDownEvent()
{
//如果按了数字键
if((event.keyCode >= 48 && event.keyCode <= 57) ||
(event.keyCode >= 96 && event.keyCode <= 105))
{
var oRange = document.selection.createRange();
if(oRange.text.indexOf(this.middleChar) != -1)
event.returnValue = false;
else
event.returnValue = true;
}
//如果按了方向键
else if(event.keyCode >= 37 && event.keyCode <= 40)
{
event.returnValue = false;
var keyCode = event.keyCode;
//按了左键
if(keyCode == 37)
{
if(this.selectIndex == 1)
{
specialText_validYear(this);
specialText_SelectDate(this);
}
else if(this.selectIndex == 2)
{
specialText_validMonth(this);
specialText_SelectYear(this);
}
else if(this.selectIndex == 3)
{
specialText_validDate(this);
specialText_SelectMonth(this);
}
}
//按了右键
if(keyCode == 39)
{
if(this.selectIndex == 1)
{
specialText_validYear(this);
specialText_SelectMonth(this);
}
else if(this.selectIndex == 2)
{
specialText_validMonth(this);
specialText_SelectDate(this);
}
else if(this.selectIndex == 3)
{
specialText_validDate(this);
specialText_SelectYear(this);
}
}

//按了上键
if(keyCode == 38)
{
if(this.selectIndex == 1)
{
specialText_validYear(this);
specialText_YearAdd(this,true);
}
else if(this.selectIndex == 2)
{
specialText_validMonth(this);
specialText_MonthAdd(this,true);
}
else if(this.selectIndex == 3)
{
specialText_validDate(this);
specialText_DateAdd(this,true);
}
}

//按了下键
if(keyCode == 40)
{
if(this.selectIndex == 1)
{
specialText_validYear(this);
specialText_YearAdd(this,false);
}
else if(this.selectIndex == 2)
{
specialText_validMonth(this);
specialText_MonthAdd(this,false);
}
else if(this.selectIndex == 3)
{
specialText_validDate(this);
specialText_DateAdd(this,false);
}
}
}
//如果按了F5 或 TAB,不屏蔽
else if(event.keyCode == 116 || event.keyCode == 9)
event.returnValue = true;
else
{
event.returnValue = false;
event.keyCode = 0;
}
}

/*---------------------辅助函数-----------------------*/
//得到默认日期
function specialText_GetDate(middleChar)
{
var oDate = new Date();
return oDate.getYear() + middleChar
+ (oDate.getMonth() < 10 ? ("0" + oDate.getMonth()) : oDate.getMonth()) + middleChar
+ (oDate.getDate() < 10 ? ("0" + oDate.getDate()) : oDate.getDate());
}
//得到字符像素宽度
function specialText_getCharWidth(charWidth,charNum)
{
return charNum * charWidth;
}
//得到某年某月的最大天数
function specialText_getMonthDates(strYear,strMonth)
{
var intMonth = parseInt(strMonth,10);
if(intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7
|| intMonth == 8 || intMonth == 10 || intMonth == 12)
return 31;
//处理30天的月份
else if(intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11)
return 30;
//处理2月份
else
{
//闰年
if(specialText_isLeapYear(strYear))
return 29;
//平年
else
return 28;
}
}
//判断是否是闰年
function specialText_isLeapYear(strYear)
{
var intYear = parseInt(strYear,10);
if((intYear % 4 == 0 && intYear % 100 != 0) ||
(intYear % 100 == 0 && intYear % 400 == 0))
return true;
else
return false;
}

function init()
{
regDateControl('date1');
}
</SCRIPT>
</HEAD>

<BODY onload="init();">
<INPUT TYPE="text" NAME="date1">
</BODY>
</HTML>
懒得去死 2006-12-27
  • 打赏
  • 举报
回复
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<SCRIPT LANGUAGE="JavaScript">
/*------------------------------------------------*/
//more javascript from http://www.smallrain.net
/*
* Added by LiuXiaoChong 2005.4.25
* 限制输入的日期控件
* Param: txtName 为要限制输入的文本框的名称
*
* 功能描述:1,只能输入数字
* 2,左右键可以移动编辑焦点
* 3,上下键可以对数据进行微调
* 4,控件包含了对日期的合法校验
*/
function regDateControl(txtName)
{
var oInput = document.getElementById(txtName);
//属性可定义?
oInput.middleChar = "-";
oInput.selectIndex = 1; //默认选中年
oInput.maxLength = 10;
oInput.style.imeMode = "disabled";
oInput.value = specialText_GetDate(oInput.middleChar);
//对这段代码有点模糊
oInput.charWidth = oInput.createTextRange().boundingWidth / oInput.maxLength;

//注册单击事件
oInput.onclick = specialText_ClickEvent;
oInput.onkeydown = specialText_KeyDownEvent;
oInput.onfocus = function(){specialText_SelectYear(this);}
oInput.onblur = function()
{
specialText_validYear(this);
specialText_validMonth(this);
specialText_validDate(this);
}
//屏蔽鼠标右键和拖动操作
oInput.oncontextmenu = function(){return false;}
oInput.ondrop = function(){return false;}
}

//鼠标单击,根据位置对日期进行分体选择
function specialText_ClickEvent()
{
event.cancelBubble = true;
specialText_validYear(this);
specialText_validMonth(this);
specialText_validDate(this);
if(event.offsetX <= specialText_getCharWidth(this.charWidth,4))
specialText_SelectYear(this);
else if(event.offsetX > specialText_getCharWidth(this.charWidth,4)
&& event.offsetX <= specialText_getCharWidth(this.charWidth,7))
specialText_SelectMonth(this);
else if(event.offsetX > specialText_getCharWidth(this.charWidth,7))
specialText_SelectDate(this);
}
//选中年份
function specialText_SelectYear(oInput)
{
var oRange = oInput.createTextRange();
oRange.moveStart("character",0);
oRange.moveEnd("character",-6);
//代表选中了年
oInput.selectIndex = 1;
oRange.select();
}
//选中月份
function specialText_SelectMonth(oInput)
{
var oRange = oInput.createTextRange();
oRange.moveStart("character",5);
oRange.moveEnd("character",-3);
//代表选中了月
oInput.selectIndex = 2;
oRange.select();
}
//选中日期
function specialText_SelectDate(oInput)
{
var oRange = oInput.createTextRange();
oRange.moveStart("character",8);
//代表选中了日期
oInput.selectIndex = 3;
oRange.select();
}
//校验年份有效性
function specialText_validYear(oInput)
{
var arrValue = oInput.value.split(oInput.middleChar);
var strYear = arrValue[0];

if(parseInt(strYear,10) == 0)
arrValue[0] = 2000;
//如果年份小于4位,则在2000基础上增加
else if(strYear.length < 4)
arrValue[0] = 2000 + parseInt(strYear,10);
oInput.value = arrValue.join(oInput.middleChar);
}
//校验月份有效性
function specialText_validMonth(oInput)
{
var arrValue = oInput.value.split(oInput.middleChar);
var strMonth = arrValue[1];
//如果月份输入了0,则按1月处理
if(parseInt(strMonth,10) == 0)
arrValue[1] = "01";
//如果月份是一位,则前面补0
else if(strMonth.length < 2)
arrValue[1] = "0" + strMonth;
//如果月份大于12月,自动转为12月
else if(parseInt(strMonth,10) > 12)
arrValue[1] = "12";
oInput.value = arrValue.join(oInput.middleChar);
}
//校验日期有效性
function specialText_validDate(oInput)
{
var arrValue = oInput.value.split(oInput.middleChar);
var strYear = arrValue[0];
var strMonth = arrValue[1];
var strDate = arrValue[2];
var intMonth = parseInt(strMonth,10);
if(parseInt(strDate,10) == 0)
arrValue[2] = "01";
else if(strDate.length < 2)
arrValue[2] = "0" + strDate;
else
{
//如果超过了月份的最大天数,则置为最大天数
var monthMaxDates = specialText_getMonthDates(strYear,strMonth);
if(parseInt(strDate,10) > monthMaxDates)
arrValue[2] = monthMaxDates;
}
oInput.value = arrValue.join(oInput.middleChar);
}

function specialText_YearAdd(oInput,isMinus)
{
var arrValue = oInput.value.split(oInput.middleChar);
var strYear = arrValue[0];
if(isMinus)
{
arrValue[0] = parseInt(strYear,10) - 1;
if(parseInt(arrValue[0],10) < 1)
arrValue[0] = "0001";
}
else
arrValue[0] = parseInt(strYear,10) + 1;
oInput.value = arrValue.join(oInput.middleChar);
specialText_validYear(oInput);
specialText_SelectYear(oInput);
}

function specialText_MonthAdd(oInput,isMinus)
{
var arrValue = oInput.value.split(oInput.middleChar);
var strMonth = arrValue[1];
if(isMinus)
{
arrValue[1] = parseInt(strMonth,10) - 1;
if(parseInt(arrValue[1],10) == 0)
arrValue[1] = "12";
}
else
{
arrValue[1] = parseInt(strMonth,10) + 1;
if(parseInt(arrValue[1],10) == 13)
arrValue[1] = "01";
}
oInput.value = arrValue.join(oInput.middleChar);
specialText_validMonth(oInput);
specialText_SelectMonth(oInput);
}

roy66 2006-12-27
  • 打赏
  • 举报
回复
<script language="JavaScript">
<!--
String.prototype.parseDate = function(){
var ar = (this + ",0,0,0").match(/\d+/g);
return ar[5]?(new Date(ar[0],ar[1]-1,ar[2],ar[3],ar[4],ar[5])):(new Date());
}

/**//*
* 功能:根据输入表达式返回日期字符串
* 参数:dateFmt:字符串,由以下结构组成
* yy:长写年,YY:短写年mm:数字月,MM:英文月,dd:日,hh:时,
* mi:分,ss秒,ms:毫秒,we:汉字星期,WE:英文星期.
* isFmtWithZero : 是否用0进行格式化,true or false
* 返回:对应日期的中文字符串
*/
Date.prototype.toString = function(dateFmt,isFmtWithZero){
dateFmt = (dateFmt == null ? "yy-mm-dd hh:mi:ss" : dateFmt);
if(typeof(dateFmt) != "string" )
throw (new Error(-1, 'toString()方法的第一个参数为字符串类型!'));
isFmtWithZero = !!isFmtWithZero;
var weekArr=[["日","一","二","三","四","五","六"],["SUN","MON","TUR","WED","THU","FRI","SAT"]];
var monthArr=["JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"];
var str=dateFmt;
var o = {
"yy" : this.getFullYear(),
"YY" : this.getYear(),
"mm" : this.getMonth()+1,
"MM" : monthArr[this.getMonth()],
"dd" : this.getDate(),
"hh" : this.getHours(),
"mi" : this.getMinutes(),
"ss" : this.getSeconds(),
"we" : "星期" + weekArr[0][this.getDay()],
"WE" : weekArr[1][this.getDay()]
}
for(var i in o){
str = str.replace(new RegExp(i,"g"),o[i].toString().fmtWithZero(isFmtWithZero));
}
str = str.replace(/ms/g,this.getMilliseconds().toString().fmtWithZeroD(isFmtWithZero));
return str;
}
/**//*将一位数字格式化成两位,如: 9 to 09*/
String.prototype.fmtWithZero = function(isFmtWithZero){
return (isFmtWithZero && /^\d$/.test(this))?"0"+this:this;
}
String.prototype.fmtWithZeroD = function(isFmtWithZero){
return (isFmtWithZero && /^\d{2}$/.test(this))?"00"+this:((isFmtWithZero && /^\d$/.test(this))?"0"+this:this);
}

/**//* 功能 : 返回与某日期相距N天(N个24小时)的日期
* 参数 : num number类型 可以为正负整数或者浮点数,默认为1;
* type 0(秒) or 1(天),默认为天
* 返回 : 新的Date对象
*/
Date.prototype.dateAfter=function(num,type){
num = (num == null?1:num);
if(typeof(num)!="number") throw new Error(-1,"dateAfterDays(num,type)的num参数为数值类型.");
type = (type==null?1:type);
var arr = [1000,86400000];
return new Date(this.valueOf() + num*arr[type]);
}
//-->
</script>
<script>
function load()
{
var sDate="2006-11-28 10:43:52";
var pDate = sDate.parseDate();
document.all.txtDate.value=(pDate.getMonth() + 1) + "-" + pDate.getDate();
}
</script>
<body onload="load()">
<div id="div1"><input type=text name='txtDate'></div>
</body>
btbtd 2006-12-27
  • 打赏
  • 举报
回复
如果是 date 对象, 那可以很容易, 不是的话...那很难.

87,926

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧