js 時間物件 字元物件

2022-09-03 08:12:16 字數 2442 閱讀 6741

var str = 'hello';  //字面量方式建立

console.log(str instanceof string); //false

var str = new string('world');

console.log(str instanceof string); //true

console.log(str instanceof object); //true

2- date物件

new date();

//獲取系統當前時間

var date1 = new date();

console.log(date1); //

//指定時間建立時間物件

var date2 = new date(2018,11,11);

console.log(date2);

var date3 = new date('2018-12-12 12:00:00');

console.log(date3);

var date4 = new date('2018/12/12 12:00:00');

console.log(date4);

var date5 = new date('tue jul 02 2019');

console.log(date5);

//獲取系統當前時間

var date1 = new date();

console.log(date1);

//獲取年份

console.log(date1.getfullyear());

//獲取月份

console.log(date1.getmonth()); // 0-11

//獲取日期

console.log(date1.getdate()); // 1-31

//獲取星期

console.log(date1.getday()); // 0-6

//獲取小時

console.log(date1.gethours()); // 0-23

//獲取分鐘

console.log(date1.getminutes()); // 0-59

//獲取秒數

console.log(date1.getseconds()); // 0-59

//獲取毫秒數

console.log(date1.getmilliseconds()); // 0-999

//獲取當前時間物件距離2023年1月1日 00:00:00的毫秒數

console.log(date1.gettime());

//獲取系統當前時間

var date1 = new date();

console.log(date1);

//設定年份

date1.setfullyear(2018);

console.log(date1);

//設定月份

date1.setmonth(7);

console.log(date1);

//設定日期

date1.setdate(3);

console.log(date1);

//設定小時,乙個小時之後的時間

date1.sethours(date1.gethours()+1);

console.log(date1);

//設定分鐘

date1.setminutes(15);

console.log(date1);

//設定秒

date1.setseconds(15);

console.log(date1);

//設定毫秒

date1.setmilliseconds(15);

console.log(date1.getmilliseconds());

var date = new date();

var d1 = date.todatestring();

console.log(d1);// 'tue jul 02 2019'

var d2 = date.totimestring();

console.log(d2); // '14:07:42 gmt+0800 (中國標準時間)'

var d3 = date.tolocaledatestring();

console.log(d3); // '2019/7/2'

var d4 = date.tolocaletimestring();

console.log(d4); // '下午2:07:42'

js時間物件

1 當前時間換時間戳 var timestamp parseint new date gettime 1000 當前時間戳 document.write timestamp 2 當前時間換日期字串 var now new date var yy now.getfullyear 年var mm now...

js 時間物件

1.date 1.單位 fullyear month date day hours minutes seconds milliseconds 2.每個單位上都有一對兒get set方法 其中 get 專門獲取單位的數值 set 專門設定單位的數值 比如 date.getdate date.getfu...

JS時間物件

獲取年 月 日 時 分 秒 var date new date 時間物件 var gmt date.togmtstring 時間物件轉換成字串,獲取格林威治時間 年 var year date.getfullyear 月 var month date.getmonth 1 外國的月份是從0開始的所以...