请问关于时间的问题,马上揭贴

ControlCoder47 2006-10-13 12:01:55
请问2006-11-10 11:11:11
这样的时间怎么得到年/月/日/小时/分钟/秒啊

...全文
189 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
梅雪香 2006-10-13
  • 打赏
  • 举报
回复

getUTCFullYear Method
This method returns a four-digit absolute number that represents the year, according to universal time, for the supplied date.

Syntax: object.getUTCFullYear( )

getUTCHours Method
This method returns an integer between 0 and 23 that represents the hours, according to universal time, in the supplied date.

Syntax: object.getUTCHours( )

getUTCMilliseconds Method
This method returns an integer between 0 and 999 that represents the milliseconds, according to universal time, in the specified date.

Syntax: object.getUTCMilliseconds( )

getUTCMinutes Method
This method returns an integer between 0 and 59 that represents the minutes, in universal time, for the supplied date.

Syntax: object.getUTCMinutes( )

getUTCMonth Method
This method returns an integer, 0 for January thru 11 for December, according to universal time, for the specified date.

Syntax: object.getUTCMonth

getUTCSeconds Method
This method returns an integer between 0 and 59 that represents the seconds, according to universal time, for the specified date.

Syntax: object.getUTCSeconds( )

parse Method
This method returns takes a date string and returns the number of milliseconds since January 01 1970 00:00:00.

Syntax: Date.parse(dateString)

setDate Method
This method is used to set the day of the month, using an integer from 1 to 31, for the supplied date according to local time.

Syntax: object.setDate(dateVal)

setFullYear Method
This method is used to set the full year for the supplied date according to local time.

Syntax: object.setFullYear(yearVal [, monthVal, dayVal])

setHours Method
This method is used to set the hours for the supplied date according to local time.

Syntax: object.setHours(hoursVal [, minutesVal, secondsVal, msVal])

setMilliseconds Method
This method is used to set the milliseconds for the supplied date according to local time. The millisecondsVal parameter expects a number between 0 and 999 athough if this is exceeded, the setMilliseconds method will automatically increment other values in the Date object, e.g. if 1020 is specified, the seconds value is incremented by one and millisecondsVal is set to 20.

Syntax: object.setMilliseconds(millisecondsVal)

setMinutes Method
This method is used to set the minutes for the supplied date according to local time.

Syntax: object.setMinutes(minutesVal [, secondsVal, msVal])

setMonth Method
This method is used to set the month for the supplied date according to local time.

Syntax: object.setMonth(monthVal [, dayVal])

setSeconds Method
This method is used to set the seconds for the specified date according to local time.

Syntax: object.setSeconds(secondsVal [, msVal)

setTime Method
This method is used to set the time of a Date object according to local time. The timeVal argument is an integer that represents the number of milliseconds elapsed since 1 January 1970 00:00:00.

Syntax: object.setTime(timeVal)

setUTCDate Method
This method is used to set the day of the month, using an integer from 1 to 31, for the supplied date according to universal time.

Syntax: object.setUTCDate(dateVal)

setUTCFullYear Method
This method is used to set the full year for the supplied date according to universal time.

Syntax: document.setUTCFullYear( yearVal [, monthVal, dayVal])

setUTCHours Method
This method is used to set the hours for the supplied date according to universal time.

Syntax: object.setUTCHours(hoursVal [, minutesVal, secondsVal, msVal])

setUTCMilliseconds Method
This method is used to set the milliseconds for the supplied date according to universal time. The millisecondsVal parameter expects a number between 0 and 999 athough if this is exceeded, the setMilliseconds method will automatically increment other values in the Date object, e.g. if 1020 is specified, the seconds value is incremented by one and millisecondsVal is set to 20.

Syntax: object.setUTCMilliseconds(millisecondsVal)

setUTCMinutes Method
This method is used to set the minutes for the supplied date according to universal time.

Syntax: object.setUTCMinutes(minutesVal [, secondsVal, msVal])

setUTCMonth Method
This method is used to set the month for the supplied date according to universal time.

Syntax: object.setUTCMonth(monthVal [, dayVal])

setUTCSeconds Method
This method is used to set the seconds for the specified date according to universal time.

Syntax: object.setUTCSeconds(secondsVal [, msVal)

toGMTString Method
This method converts a local date to Greenwich Mean Time.

Syntax: object.toGMTString( )

toLocaleString Method
This method uses the relevant locale's date conventions when converting a date to a string.

Syntax: object.toLocaleString( )

toSource Method
This method is used to return the source code that created the specified Date object.

Syntax: object.toSource( )

toString Method
This method returns a string that represents the Date object. This method is automatically called by JavaScript whenever a Date object needs to be displayed as text (as with many of the other methods of the Date object).

Syntax: object.toString( )

toUTCString Method
This method uses the universal time convention when converting a date to a string.

Syntax: object.toUTCString( )

UTC Method
This method returns the number of milliseconds from the date in a Date object since January 1, 1970 00:00:00 according to universal time. This is a static method of Date so the format is always Date.UTC() as opposed to objectName.UTC().

Syntax: Date.UTC(year, month, day [, hours, minutes, seconds, ms])

valueOf Method
This method is used to return a primitive value, as a number data type, of the specified Date object. This is returned as the number of millisecond since January 1, 1970 00:00:00.

Syntax: object.valueOf( )
梅雪香 2006-10-13
  • 打赏
  • 举报
回复
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());
}

OBJECT: Date

--------------------------------------------------------------------------------
new Date()>

new Date(milliseconds)

new Date(dateString)

new Date(yr_num, mo_num, day_num [, hr_num, min_num,
sec_num, ms_num])

The Date object allows you to work programatically with dates and times. You create a Date object using the Date constructor as shown in the syntax above and the available parameters are as follows:

milliseconds - an integer that represents the number of milliseconds since 01/01/70 00:00:00.

dateString - a string that represents the date in a format that is recognized by the Date.parse method.

yr_num, mo_num, day_num - an integer that represents the year, month or day of the date.

hr_num, min_num, sec_num, ms_num - an integer that represents the hours, minutes, seconds and milliseconds.

If you don't supply any of the above parameters, JavaScript creates an object for today's date according to the time on the local machine. If any arguments are supplied, you have to include the year, month and day as a minimum, with the time parameters being optional. Note that if you only supply some arguments, any not supplied are set to 0.

All dates are calculated in milliseconds from 01 January, 1970 00:00:00 Universal Time (UTC) with a day containing 86,400,000 milliseconds. The range of a Date object relative to 01/01/1970 (UTC) is -100,000,000 to 100,000,000 days and both Universal (UTC) time and Greenwich Mean Time (GMT) are supported.

The following code uses Date objects to calculate the time remaining, in days, to the start of the next millennium.

Code:
d = new Date() //today's date
mill=new Date(3000, 00, 01, 00, 00, 00) //Next millennium start date
diff = mill-d //difference in milliseconds
mtg = new String(diff/86400000) //calculate days and convert to string
point=mtg.indexOf(".") //find the decimal point
days=mtg.substring(0,point) //get just the whole days
document.write("There are only " + days + " days remaining to the start of the next millennium.")

Output:
There are only 365033 days remaining to the start of the next millennium.


PROPERTIES

constructor Property
This property returns a reference to the function that created the Date object's prototype.

Syntax: object.constructor

prototype Property
This property represents the prototype for the object's class and can be used to add properties and methods to all instances of that class.

Syntax: object.prototype


METHODS

getDate Method
This method returns an integer (between 1 and 31) representing the day of the month for the specified (local time) date.

Syntax: object.getDate( )

getDay Method
This method returns an integer (0 for Sunday thru 6 for Saturday) representing the day of the week.

Syntax: object.getDay( )

getFullYear Method
This method returns an integer representing the year of a specified date. The integer returned is a four digit number, 1999, for example, and this method is to be prefered over getYear.

Syntax: object.getFullYear( )

getHours Method
This method returns an integer between 0 and 23 that represents the hour (local time) for the specified date.

Syntax: object.getHours( )

getMilliseconds Method
This method returns an integer between 0 and 999 that represents the milliseconds (local time) for the specified date.

Syntax: object.getMilliseconds( )

getMinutes Method
This method returns an integer between 0 and 59 that represents the minutes (local time) for the specified date.

Syntax: object.getMinutes( )

getMonth Method
This method returns an integer (0 for January thru 11 for December) that represents the month for the specified date.

Syntax: object.getMonth( )

getSeconds Method
This method returns an integer between 0 and 59 that represents the seconds (local time) for the specified date.

Syntax: object.getSeconds( )

getTime Method
This method returns a numeric value representing the number of milliseconds since midnight 01/01/1970 for the specified date.

Syntax: object.getTime( )

getTimezoneOffset Method
This method returns the difference in minutes between local time and Greenwich Mean Time. This value is not a constant, as you might think, because of the practice of using Daylight Saving Time.

Syntax: object.getTimezoneOffset( )

getUTCDate Method
This method returns an integer between 1 and 31 that represents the day of the month, according to universal time, for the specified date.

Syntax: object.getUTCDate( )

getUTCDay Method
This method returns an integer (0 for Sunday thru 6 for Saturday) that represents the day of the week, according to universal time, for the specified date.

Syntax: object.getUTCDay( )
KKK111000 2006-10-13
  • 打赏
  • 举报
回复
<script type=text/javascript>
var dateFormat = /(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/;
var str="2006-11-10 11:11:11";
var res = str.match(dateFormat);
var out = "";
out += "年:"+res[1]+"\n";
out += "月:"+res[2]+"\n";
out += "日:"+res[3]+"\n";
out += "时:"+res[4]+"\n";
out += "分:"+res[5]+"\n";
out += "秒:"+res[6]+"\n";
alert(out);
</script>
mingxuan3000 2006-10-13
  • 打赏
  • 举报
回复
bb=aa[0].split("-")
cc=aa[1].split(":")
mingxuan3000 2006-10-13
  • 打赏
  • 举报
回复
var str="2006-11-10 11:11:11"
var aa=str.split(" ")
bb=aa[0].split("-")
cc=aa[0].split(":")
alert(bb)
alert(cc)

liufei8463 2006-10-13
  • 打赏
  • 举报
回复
xue xi
ControlCoder47 2006-10-13
  • 打赏
  • 举报
回复
hbhbhbhbhb1021(天外水火(我要多努力)) ( ) 信誉:100 Blog
------------------

你误会我的意思了

我要得出

年:2006
月:11
日:10

小时:
.....

要得出具体的每个数字
ControlCoder47 2006-10-13
  • 打赏
  • 举报
回复
zhaoxiaoyang(梅雪香@深圳) ( ) 信誉:100 Blog
-------------------------------------------

拜托

也这么多星星了,不要老是贴代码好不好
hbhbhbhbhb1021 2006-10-13
  • 打赏
  • 举报
回复
<script language=javascript>
var str="2006-11-10 11:11:11"
var strArray1=new Array('年','月','小时','分')
var i=0;
str=str.replace(/-/g,function(){return strArray1[i++]})
str=str.replace(/:/g,function(){return strArray1[i++]})
str=str.replace(" ","日")
str=str.replace(/$/,"秒")
alert(str)
</script>
梅雪香 2006-10-13
  • 打赏
  • 举报
回复
<script language="JavaScript">
<!--
//程序:常用公历日期处理程序
//作者:梅雪香(meixx)
//时间:忘了
/*用相对不规则的字符串来创建日期对象,不规则的含义为:顺序包含年月日三个数值串,有间隔*/
String.prototype.parseDate = function(){
var ar = (this + ",0,0,0").match(/\d+/g);
return ar?(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]);
}

//判断是否是闰年,返回true 或者 false
Date.prototype.isLeapYear = function (){
var year = this.getFullYear();
return (0==year%4 && ((year % 100 != 0)||(year % 400 == 0)));
}

//返回该月天数
Date.prototype.getDaysOfMonth = function (){
return (new Date(this.getFullYear(),this.getMonth()+1,0)).getDate();
}

//日期比较函数,参数date:为Date类型,如this日期晚于参数:1,相等:0 早于: -1
Date.prototype.dateCompare = function(date){
if(typeof(date) != "object" || !(/Date/.test(date.constructor)))
throw new Error(-1,"dateCompare(date)的date参数为Date类型.");
var d = this.getTime() - date.getTime();
return d>0?1:(d==0?0:-1);
}

/*功能:返回两日期之差
*参数:pd PowerDate对象
* type: 返回类别标识.yy:年,mm:月,ww:周,dd:日,hh:小时,mi:分,ss:秒,ms:毫秒
* intOrFloat :返回整型还是浮点型值 0:整型,不等于0:浮点型
* output : 输出提示,如:时间差为#周!
*/
Date.prototype.calDateDistance = function (date,type,intOrFloat,output){
if(typeof(date) != "object" || !(/Date/.test(date.constructor)))
throw new Error(-1,"calDateDistance(date,type,intOrFloat)的date参数为Date类型.");
type = (type==null?'dd':type);
if(!((new RegExp(type+",","g")).test("yy,mm,ww,dd,hh,mi,ss,ms,")))
throw new Error(-1,"calDateDistance(pd,type,intOrFloat,output)的type参数为非法.");
var iof = (intOrFloat==null?0:intOrFloat);
var num=0;
var o = {
"ww" : 7*86400000,
"dd" : 86400000,
"hh" : 3600000,
"mi" : 60000,
"ss" : 1000,
"ms" : 1
}
switch(type){
case "yy": num = this.getFullYear() - date.getFullYear(); break;
case "mm": num = (this.getFullYear() - date.getFullYear())*12+this.getMonth()-date.getMonth(); break;
default:
var sub = this.valueOf() - date.valueOf();
if (o[type])
num = (sub/o[type]).fmtRtnVal(iof);
break;
}
return (output ? output.replace(/#/g," "+num+" ") : num);
}
//返回整数或者两位小数的浮点数
Number.prototype.fmtRtnVal = function (intOrFloat){
return (intOrFloat == 0 ? Math.floor(this) : parseInt(this*100)/100);
}
//根据当前日期所在年和周数返回周日的日期
Date.prototype.RtnByWeekNum = function (weekNum){
if(typeof(weekNum) != "number")
throw new Error(-1,"RtnByWeekNum(weekNum)的参数是数字类型.");
var date = new Date(this.getFullYear(),0,1);
var week = date.getDay();
week = (week==0?7:week);
return date.dateAfter(weekNum*7-week,1);
}
//根据日期返回该日期所在年的周数
Date.prototype.getWeekNum = function (){
var dat = new Date(this.getFullYear(),0,1);
var week = dat.getDay();
week = (week==0?7:week);
var days = this.calDateDistance(dat,"dd")+1;
return ((days + 6 - this.getDay() - 7 + week)/7);
}

//-->
</script>

<script language="JavaScript">
<!--
var dw = document.write;
var date2 = "2005-12-30".parseDate();
dw(date2,"<br>");
var date3 = "2006-1-10 11 11 11".parseDate();
dw(date2.calDateDistance(date3,null,null,"时间差为#天!"));
//alert(new Date(2000,2,-29));
//-->
</script>
ControlCoder47 2006-10-13
  • 打赏
  • 举报
回复
carlxuan 2006-10-13
  • 打赏
  • 举报
回复
<script>
var a = "2006-11-10 11:11:11";
var b = new Array();
b = a.split(/[-\s:]/);
alert(b[0]);
alert(b[1]);
alert(b[2]);
</script>

87,921

社区成员

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

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