導航:首頁 > 編程語言 > jsdateprototype

jsdateprototype

發布時間:2023-02-08 03:02:20

js中datetime類型

js中格式化日期時間型數據函數代碼如下:

例如:
現有一字元串為:
var dt="2010-1-1 12:20:20";
第一步:將其轉換成日期時間型數據

varnewDt=newDate(dt.replace("-","/"));

第二步:格式化數據成"yyyy-MM-dd"

(1)擴展Date

Date.prototype.format=function(format){
varo={
"M+":this.getMonth()+1,//month
"d+":this.getDate(),//day
"h+":this.getHours(),//hour
"m+":this.getMinutes(),//minute
"s+":this.getSeconds(),//second
"q+":Math.floor((this.getMonth()+3)/3),//quarter
"S":this.getMilliseconds()//millisecond
};
if(/(y+)/.test(format)){
format=format.replace(RegExp.$1,(this.getFullYear()+"").substr(4-RegExp.$1.length));
}
for(varkino){
if(newRegExp("("+k+")").test(format)){
format=format.replace(RegExp.$1,RegExp.$1.length==1?o[k]:("00"+o[k]).substr((""+o[k]).length));
}
}
returnformat;
};

(2)使用format格式化時間

varfmtDt=newDt.format("yyyy-MM-dd");

⑵ js 如何把字元串轉化為日期

varstr="2010-08-01";

//轉換日期格式

str=str.replace(/-/g,'/');//"2010/08/01";

//創建日期對象

vardate=newDate(str);

//加一天

date.setDate(date.getDate()+1);

(2)jsdateprototype擴展閱讀:

反之,日期格式化為字元串

//完整的格式化 var time2 = new Date().format("yyyy-MM-dd hh:mm:ss");

Date.prototype.format = function (fmt) {

var o = {

"M+": this.getMonth() + 1, //月份

"d+": this.getDate(), //日

"h+": this.getHours(), //小時

"m+": this.getMinutes(), //分

"s+": this.getSeconds(), //秒

"q+": Math.floor((this.getMonth() + 3) / 3), //季度

"S": this.getMilliseconds() //毫秒

};

//處理年份

var reYear = /(y+)/;

var resultYear = reYear.exec(fmt);

if (resultYear)

{

var yearformatPart = resultYear[0];//匹配到的格式化字元

var yearVal = (this.getFullYear() + "").substr(4 - yearformatPart.length);

fmt = fmt.replace(yearformatPart, yearVal);

}

for (var k in o) {

var re = new RegExp("(" + k + ")");

var re = re.exec(fmt);

if (re) {

var Val = "" + o[k];//本次需要替換的數據

var formatPart = re[0];//匹配到的格式化字元

var replaceVal = (formatPart.length == 1) ? (Val) : (("00" + Val).substr(Val.length));

fmt = fmt.replace(formatPart, replaceVal);

}

}

return fmt;

}

⑶ js prototype是什麼

prototype是一個屬性,所有的抄函數都有,襲這個屬性引用了一個對象,即原型對象,簡稱原型。所以大多數把prototype之間叫原型。

通過原型可以實現方法或者屬性擴充

比如

functionA(){
}
A.prototype.show=function(){//擴充函數A的方法show
alert('1');
}

實現原型鏈方式的繼承。

functionB(x,y){
this.y=y;
}
B.prototype=newA();//這是b也有show方法

⑷ 怎麼讓js中date進行加減運算

js中沒有如同C#中的AddDays的方法,
所以重寫了對象的prototype,擴展了增加日期的方法,

代碼如下:
Date.prototype.Format = function(fmt)
{
//author:
meizz
var o
=

{

"M+" : this.getMonth() + 1, //月份

"d+" : this.getDate(), //日

"h+" : this.getHours(), //小時

"m+" : this.getMinutes(), //分

"s+" : this.getSeconds(), //秒

"q+" : Math.floor((this.getMonth() + 3) / 3), //季度

"S" : this.getMilliseconds() //毫秒

};
if
(/(y+)/.test(fmt))

fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 -
RegExp.$1.length));
for (var k
in o)

if (new RegExp("(" + k + ")").test(fmt))

fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) :
(("00" + o[k]).substr(("" + o[k]).length)));
return
fmt;
}

Date.prototype.addDays = function(d)
{

this.setDate(this.getDate() + d);
};

Date.prototype.addWeeks = function(w)
{

this.addDays(w * 7);
};

Date.prototype.addMonths= function(m)
{
var d =
this.getDate();

this.setMonth(this.getMonth() + m);

if
(this.getDate() < d)

this.setDate(0);
};

Date.prototype.addYears = function(y)
{
var m =
this.getMonth();

this.setFullYear(this.getFullYear() + y);

if (m
< this.getMonth())

{

this.setDate(0);

}
};

var now = new Date();
now.addDays(1);//加減日期操作
alert(now.Format("yyyy-MM-dd"));

⑸ js 中怎麼獲取當前系統時間

系統時間一般是值服務端時間,js獲取服務端時間的方法是直接用獲取。

編寫顯示時間的頁面:

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Server date/time</title>

<script language="javascript" src="serverDate.js"></script>

</head>

<script language="javascript">

var localTime = new Date();

document.write("Local machine time is: " + localTime + "<br>");

document.write("Server time is: " + date);

</script>

<body>

</body>

ajax腳本獲取server的時間

var xmlHttp;

function srvTime(){

try {

//創建xmlHttp對象

xmlHttp = new XMLHttpRequest();

}

catch (err1) {

//ie瀏覽器

try {

xmlHttp = new ActiveXObject('Msxml2.XMLHTTP');

}

catch (err2) {

try {

xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');

}

catch (eerr3) {

//ajax不支持

alert("AJAX not supported");

}

}

}

//打開xmlHttp請求

xmlHttp.open('HEAD',window.location.href.toString(),false);

//設置xmlHttp請求頭

xmlHttp.setRequestHeader("Content-Type", "text/html");

//發送請求

xmlHttp.send('');

// 獲取response中的Date參數

return xmlHttp.getResponseHeader("Date");

}

var st = srvTime(); //伺服器時間賦值給st變數

var date = new Date(st); //轉換js的date對象

// 輸出伺服器時間

document.write("伺服器時間: " + date);

拓展資料:

JavaScript一種直譯式腳本語言,是一種動態類型、弱類型、基於原型的語言,內置支持類型。它的解釋器被稱為JavaScript引擎,為瀏覽器的一部分,廣泛用於客戶端的腳本語言,最早是在HTML(標准通用標記語言下的一個應用)網頁上使用,用來給HTML網頁增加動態功能。

語句:JavaScript程序是由若干語句組成的,語句是編寫程序的指令。JavaScript提供了完整的基本編程語句,

它們是:賦值語句、switch選擇語句、while循環語句、for循環語句、for each循環語句、do...while循環語句、break循環中止語句、continue循環中斷語句、with語句、try…catch語句、if語句(if..else,if…else if…)。

⑹ 怎麼在js里把date轉換為字元串

/***對Date的擴展,將Date轉化為指定格式的String*月(M)、日(d)、12小時(h)、24小時(H)、分(m)、秒(s)、周(E)、季度(q)
可以用1-2個佔位符*年(y)可以用1-4個佔位符,毫秒(S)只能用1個佔位符(是1-3位的數字)*eg:*(new
Date()).pattern("yyyy-MM-ddhh:mm:ss.S")==>2006-07-0208:09:04.423
*(newDate()).pattern("yyyy-MM-ddEHH:mm:ss")==>2009-03-10二20:09:04
*(newDate()).pattern("yyyy-MM-ddEEhh:mm:ss")==>2009-03-10周二08:09:04
*(newDate()).pattern("yyyy-MM-ddEEEhh:mm:ss")==>2009-03-10星期二08:09:04
*(newDate()).pattern("yyyy-M-dh:m:s.S")==>2006-7-28:9:4.18
*/
Date.prototype.pattern=function(fmt){
varo={
"M+":this.getMonth()+1,//月份
"d+":this.getDate(),//日
"h+":this.getHours()%12==0?12:this.getHours()%12,//小時
"H+":this.getHours(),//小時
"m+":this.getMinutes(),//分
"s+":this.getSeconds(),//秒
"q+":Math.floor((this.getMonth()+3)/3),//季度
"S":this.getMilliseconds()//毫秒
};
varweek={
"0":"/u65e5",
"1":"/u4e00",
"2":"/u4e8c",
"3":"/u4e09",
"4":"/u56db",
"5":"/u4e94",
"6":"/u516d"
};
if(/(y+)/.test(fmt)){
fmt=fmt.replace(RegExp.$1,(this.getFullYear()+"").substr(4-RegExp.$1.length));
}
if(/(E+)/.test(fmt)){
fmt=fmt.replace(RegExp.$1,((RegExp.$1.length>1)?(RegExp.$1.length>2?"/u661f/u671f":"/u5468"):"")+week[this.getDay()+""]);
}
for(varkino){
if(newRegExp("("+k+")").test(fmt)){
fmt=fmt.replace(RegExp.$1,(RegExp.$1.length==1)?(o[k]):(("00"+o[k]).substr((""+o[k]).length)));
}
}
returnfmt;
}

vardate=newDate();
window.alert(date.pattern("yyyy-MM-ddhh:mm:ss"));

⑺ js的日期格式化函數是啥!!!

js沒有日期格式化函數,想要日期格式化,就自己寫一個。

/**
* 時間對象的格式化;
*/
Date.prototype.format = function(format) {
/*
* eg:format="YYYY-MM-dd hh:mm:ss";
*/
var o = {
"M+" :this.getMonth() + 1, // month
"d+" :this.getDate(), // day
"h+" :this.getHours(), // hour
"m+" :this.getMinutes(), // minute
"s+" :this.getSeconds(), // second
"q+" :Math.floor((this.getMonth() + 3) / 3), // quarter
"S" :this.getMilliseconds()
// millisecond
}
if (/(y+)/.test(format)) {
format = format.replace(RegExp.$1, (this.getFullYear() + "")
.substr(4 - RegExp.$1.length));
}
for ( var k in o) {
if (new RegExp("(" + k + ")").test(format)) {
format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k]
: ("00" + o[k]).substr(("" + o[k]).length));
}
}
return format;
}

閱讀全文

與jsdateprototype相關的資料

熱點內容
一部電影日本跟學校有關 瀏覽:595
查詢寧鄉公交用什麼app 瀏覽:371
cms網站模板 瀏覽:456
有什麼電影露奶子 瀏覽:907
S7edge支持微信指紋么 瀏覽:83
win10跨平台 瀏覽:343
qq暴走gif動態圖片製作 瀏覽:340
iphone5連不上網路數據 瀏覽:229
佛教電影大全佛片 瀏覽:191
如何還原回收站的文件 瀏覽:850
洛陽大數據產業園區 瀏覽:900
伺服器資料庫在哪裡 瀏覽:769
割奶電影免推薦 瀏覽:376
數控刀架怎麼編程讓它一直轉 瀏覽:311
app創意名字大全 瀏覽:186
黑太陽731小啞巴扮演者 瀏覽:485
寧夏中考成績查詢網站是多少 瀏覽:786
主角把自己媽媽和姐姐收了 瀏覽:521
taz是什麼網站 瀏覽:479
怎麼登陸網站伺服器 瀏覽:957

友情鏈接