iT邦幫忙

2021 iThome 鐵人賽

DAY 29
0
自我挑戰組

Be friend with JavaScript系列 第 29

Day 29 - Math Object & Date Object

Math Object

methods

  • Math.PI : 3.14
  • Math.LOG10E : 以 10 為底的 E 的對數
  • Math.SQRT2 : 2 的平方根

properties

  • Math.pow(a,b)
    return a 的 b 次方
let ans = Math.pow(2,10);
console.log(ans); // 1024
  • Math.random()
    return 0 ~ 1 之間隨機一個數字
    包含 0,但不包含 1(介於 0 ~ 0.99999999....)
let ans1 = Math.random();
let ans2 = Math.random();
console.log(ans1); // 0.8127689903854909
console.log(ans2); // 0.06143377179016474
  • Math.sqrt()
    return 平方根
let ans = Math.sqrt(64);
console.log(ans); // 8
  • Math.abs()
    return 絕對值
let ans = Math.abs(-30);
console.log(ans); // 30
  • Math.floor()
    retrun 無條件捨去(小數點全部去掉)
let ans = Math.floor(3.141592);
console.log(ans); // 3
  • Math.ceil()
    retrun 無條件進位
let ans = Math.ceil(4.0000001);
console.log(); // 5
  • Math.round(x)
    return x 的四捨五入值
let ans = Math.round(26.853);
console.log(ans); // 27

Date Object

  • 語法:new Date(),若沒有傳入任何參數,則預設為當下時間,以函數方式呼叫 Date 時,會 return 當下時間日期的字串。

new Date() 可以放入 7 個參數,指定年、月、日、小時、分鐘、秒、毫秒(按照順序)

例如:

// 無參數,回傳現在時間
console.log(new Date());
// Sat Oct 02 2021 03:47:31 GMT+0800 (台北標準時間)

let time = new Date(2021,10,2,12,35,22);
// 要注意月份是從 0  開始,所以當我們月份的參數寫 10 時,結果會是 11 月。
console.log(time); // Tue Nov 02 2021 12:35:22 GMT+0800 (台北標準時間)

月份為 0-11,如果我們把月份的參數設為 12,則會自動進位變成下個年度的 1 月,例如:

let time2 = new Date(2020,12,31,12,30,33);
console.log(time2); // Sun Jan 31 2021 12:30:33 GMT+0800 (台北標準時間)
  • 所有 Date 物件實體繼承自 Date.prototype 。這個 Date 建構子的 prototype 物件可以被修改以影響所有 Date 物件實體。
  • Date.prototype 方法有 Getter(回傳時間), Setter(設定時間), Conversion getter(將時間轉換格式回傳) 等,這邊只討論 Getter,常用的方法有 getDate(), getDay(), getFullYear(), getHours(), getMinutes(), getMonth(), getSeconds(), getMilliseconds()等。

getFullYear()

return 西元幾年 (yyyy)

let time = new Date();
let year = time.getFullYear();
console.log(year); // 2021

getMonth()

return 月份 (0-11)
現在是 10 月,所以會 return 9

let time = new Date();
let month = time.getMonth();
console.log(month); // 9

getDate()

return 日期 (1-31)

let time = new Date();
let date = time.getDate();
console.log(date); // 2

getDay()

return 星期幾 (0-6) - 星期日:0~星期六:6
今天星期六,return 6

let time = new Date();
let day = time.getDay();
console.log(day); // 6

getHours()

return 幾點 (0-23)

let time = new Date();
let hour = time.getHours();
console.log(time); // Sat Oct 02 2021 04:19:25 GMT+0800 (台北標準時間)
console.log(hour); // 4

getMinutes()

return 幾分 (0-59)

let time = new Date();
let minute = time.getMinutes();
console.log(time); // Sat Oct 02 2021 04:20:17 GMT+0800 (台北標準時間)
console.log(minute); // 20

getSeconds()

return 幾秒 (0-59)

let time = new Date();
let second = time.getSeconds();
console.log(time); // Sat Oct 02 2021 04:29:17 GMT+0800 (台北標準時間)
console.log(second); // 17

最後再舉個例子:
假如我們想要把現在的日期轉換為中文字,星期幾 & 幾月,可以先宣告星期和月份的陣列,再用索引值去抓

const time = new Date();
const weekday = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
const month = ["1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"];
console.log(time); // Sat Oct 02 2021 04:31:40 GMT+0800 (台北標準時間)
console.log(weekday[time.getDay()]); // 星期六
console.log(month[time.getMonth()]); // 10月

參考資料:
Math - MDN
JavaScript Date Objects - w3schools


上一篇
Day 28 - 使用各種方式取得資料
下一篇
Day 30 - Module
系列文
Be friend with JavaScript39
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言