js獲取日期時間

2022-09-13 14:51:11 字數 1598 閱讀 5593

document.write(date());

//獲取當前完整日期時間

var a=new date();

var my_day=a.getdate();

var my_month=a.getmonth()+1;

var my_year=a.getfullyear();

document.write(my_year+"年"+my_month+"月"+my_day+"日");

獲取系統當前日期,如果不用字年月日,時間就會相加, 

getmonth加1 是因為系統是按照0開始計算月份

//2018+9+6=2032

var a=new date();

a.setfullyear(2018,9,6);

document.write(a);

設定物件年月日,記住月份要比你想要的月份設定的小一月,年是必須設定,月和日是可選的

setfullyear可以設定年月日

var a=new date();

a.setmonth(9,6);

document.write(a); 設定物件月,日,

日期物件.setdate(day)設定日

vard =newdate();

varmy_hours=d.gethours();

varmy_minutes=d.getminutes();

varmy_seconds=d.getseconds();

document.write("當前時間是:"+my_hours+":"+my_minutes+":"+my_seconds);

獲取系統當前的時分秒

var a=new date();

a.sethours(18,45,59)

document.write(a);設定物件的小時 ,分, 秒

日期物件.setminutes(min,sec,millisec) 設定分鐘和秒數,秒數可選

日期物件.setseconds(sec,millisec)設定秒數,毫秒數可選

var a=new date();

document.write(a.tostring());將date獲得的日期變成字串

vard =newdate();

document.write(d.toutcstring()); 將日期變成utc世界時間字串表示

var d = new date();

document.write(d.tolocalestring());把date時間變成本地時間字串顯示

JS日期,JS獲取當前日期時間,js日期格式化

var mydate new date mydate.getyear 獲取當前年份 2位 mydate.getfullyear 獲取完整的年份 4位,1970 mydate.getmonth 獲取當前月份 0 11,0代表1月 注意別忘了 1 mydate.getdate 獲取當前日 1 31 my...

Js獲取當前日期時間

var mydate new date mydate.getyear 獲取當前年份 2位 mydate.getfullyear 獲取完整的年份 4位,1970 mydate.getmonth 獲取當前月份 0 11,0代表1月 mydate.getdate 獲取當前日 1 31 mydate.get...

JS獲取當前日期時間

var mydate new date var years mydate.getfullyear 從date 物件以四位數字返回年份 var months mydate.getmonth 從date 物件返回月份 0 11 var days mydate.getdate 從date物件返回乙個月中的...