Lembaran Cheat Objek Tarikh JavaScript
Dec 01, 2024 pm 07:03 PMObjek tarikh dalam JavaScript digunakan untuk berfungsi dengan tarikh dan masa. Ia menyediakan kaedah untuk mencipta, memanipulasi dan memformat nilai tarikh dan masa.
Mencipta Tarikh
Anda boleh mencipta objek Tarikh dalam pelbagai cara:
- Tarikh dan Masa Semasa:
const now = new Date(); console.log(now); // Current date and time
- Tarikh Tertentu:
const specificDate = new Date(2024, 10, 21); // Year, Month (0-based), Day console.log(specificDate); // Thu Nov 21 2024
- Dari Rentetan:
const fromString = new Date("2024-11-21T10:00:00"); console.log(fromString); // Thu Nov 21 2024 10:00:00 GMT
- Dari Cap Masa (milisaat sejak zaman Unix):
const fromTimestamp = new Date(1732231200000); console.log(fromTimestamp); // Thu Nov 21 2024 10:00:00 GMT
Kaedah Biasa
Mendapatkan Tarikh dan Masa
Method | Description | Example |
---|---|---|
getFullYear() | Returns the year | date.getFullYear() -> 2024 |
getMonth() | Returns the month (0-11) | date.getMonth() -> 10 (November) |
getDate() | Returns the day of the month (1-31) | date.getDate() -> 21 |
getDay() | Returns the weekday (0-6, Sun=0) | date.getDay() -> 4 (Thursday) |
getHours() | Returns the hour (0-23) | date.getHours() -> 10 |
getMinutes() | Returns the minutes (0-59) | date.getMinutes() -> 0 |
getSeconds() | Returns the seconds (0-59) | date.getSeconds() -> 0 |
getTime() | Returns timestamp in milliseconds | date.getTime() -> 1732231200000 |
Penerangan
Method | Description | Example |
---|---|---|
setFullYear(year) | Sets the year | date.setFullYear(2025) |
setMonth(month) | Sets the month (0-11) | date.setMonth(0) -> January |
setDate(day) | Sets the day of the month | date.setDate(1) -> First day of the month |
setHours(hour) | Sets the hour (0-23) | date.setHours(12) |
setMinutes(minutes) | Sets the minutes (0-59) | date.setMinutes(30) |
setSeconds(seconds) | Sets the seconds (0-59) | date.setSeconds(45) |
Memformat Tarikh
Method | Description | Example |
---|---|---|
toDateString() | Returns date as a human-readable string | date.toDateString() -> "Thu Nov 21 2024" |
toISOString() | Returns date in ISO format | date.toISOString() -> "2024-11-21T10:00:00.000Z" |
toLocaleDateString() | Returns date in localized format | date.toLocaleDateString() -> "11/21/2024" |
toLocaleTimeString() | Returns time in localized format | date.toLocaleTimeString() -> "10:00:00 AM" |
Penerangan
- Kes Penggunaan Biasa
const now = new Date(); console.log(now); // Current date and time
- Kira Hari Antara Dua Tarikh
- :
const specificDate = new Date(2024, 10, 21); // Year, Month (0-based), Day console.log(specificDate); // Thu Nov 21 2024
- Pemasa Kira detik
- :
const fromString = new Date("2024-11-21T10:00:00"); console.log(fromString); // Thu Nov 21 2024 10:00:00 GMT
- Formatkan Tarikh Semasa
- :
const fromTimestamp = new Date(1732231200000); console.log(fromTimestamp); // Thu Nov 21 2024 10:00:00 GMT
- Cari Hari dalam Minggu
- :
const startDate = new Date("2024-11-01"); const endDate = new Date("2024-11-21"); const diffInTime = endDate - startDate; // Difference in milliseconds const diffInDays = diffInTime / (1000 * 60 * 60 * 24); // Convert to days console.log(diffInDays); // 20
- Semak Tahun Lompat
- :
const targetDate = new Date("2024-12-31T23:59:59"); setInterval(() => { const now = new Date(); const timeLeft = targetDate - now; // Milliseconds left const days = Math.floor(timeLeft / (1000 * 60 * 60 * 24)); const hours = Math.floor((timeLeft / (1000 * 60 * 60)) % 24); const minutes = Math.floor((timeLeft / (1000 * 60)) % 60); const seconds = Math.floor((timeLeft / 1000) % 60); console.log(`${days}d ${hours}h ${minutes}m ${seconds}s`); }, 1000);
Tambah/Tolak Hari
:
- Petua Pro
const now = new Date(); const formatted = `${now.getFullYear()}-${now.getMonth() + 1}-${now.getDate()}`; console.log(formatted); // "2024-11-21"
- Date.now()
- untuk mendapatkan cap masa semasa secara langsung tanpa membuat objek Date:
Berhati-hati dengan zon waktu
apabila bekerja dengan tarikh merentas wilayah. Gunakan perpustakaan seperti Moment.js - atau
Day.js untuk pengendalian lanjutan.
0 diindeks
Atas ialah kandungan terperinci Lembaran Cheat Objek Tarikh JavaScript. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Artikel Panas

Alat panas Tag

Artikel Panas

Tag artikel panas

Notepad++7.3.1
Editor kod yang mudah digunakan dan percuma

SublimeText3 versi Cina
Versi Cina, sangat mudah digunakan

Hantar Studio 13.0.1
Persekitaran pembangunan bersepadu PHP yang berkuasa

Dreamweaver CS6
Alat pembangunan web visual

SublimeText3 versi Mac
Perisian penyuntingan kod peringkat Tuhan (SublimeText3)

Topik panas

Ganti aksara rentetan dalam javascript

Tutorial Persediaan API Carian Google Custom

8 plugin susun atur halaman jquery yang menakjubkan
