Apps Artificial Intelligence CSS DevOps Go JavaScript Laravel Linux MongoDB MySQL PHP Python Rust Vue

JavaScript: Cara Cepat Format Tanggal

1 min read .
JavaScript: Cara Cepat Format Tanggal

Kadang di proyek web, saya butuh format tanggal yang rapi. JavaScript punya banyak cara, dari built-in sampai library.

1. toLocaleDateString()

const date = new Date();
console.log(date.toLocaleDateString()); // "8/28/2024" (tergantung locale)

const options = { year:'numeric', month:'long', day:'numeric' };
console.log(date.toLocaleDateString('en-US', options)); // "August 28, 2024"

2. toISOString() (ISO 8601)

console.log(new Date().toISOString()); // "2024-08-28T12:34:56.789Z"

3. Format Manual

const d = new Date();
const year = d.getFullYear();
const month = String(d.getMonth()+1).padStart(2,'0');
const day = String(d.getDate()).padStart(2,'0');
console.log(`${year}-${month}-${day}`); // "2024-08-28"

4. Intl.DateTimeFormat

const formatter = new Intl.DateTimeFormat('en-GB', {
  year:'numeric', month:'short', day:'numeric', weekday:'long'
});
console.log(formatter.format(new Date())); // "Wednesday, 28 Aug 2024"

5. Library

  • Moment.js
import moment from 'moment';
console.log(moment().format('MMMM Do YYYY, h:mm:ss a')); // "August 28th 2024, 12:34:56 pm"
  • date-fns
import { format } from 'date-fns';
console.log(format(new Date(),'do MMMM yyyy')); // "28th August 2024"

Tips

  • Gunakan library ringan kalau perlu.
  • Perhatikan time zone.
  • Pertahankan format konsisten di seluruh aplikasi.

Kesimpulan

Pada akhirnya, dari built-in sampai library, JavaScript fleksibel buat format tanggal. Gini deh, kadang hal kecil kayak ini bikin tampilan data jauh lebih rapi dan profesional.

Lihat Juga

HTTP Requests di JavaScript dengan Fetch API
HTTP Requests di JavaScript dengan Fetch API
Fetch API itu modern, clean, dan promise-based—solusi yang jauh lebih enak dibanding XMLHttpRequest. Gini deh, contoh CRUD pakai DummyJSON API. 1. Ambil Semua Produk Copy fetch('https://dummyjson.com/products') .then(res => res.json()) .then(console.log) .catch(err => console.error('Error:', err)); 2. Ambil Produk Berdasarkan ID Copy fetch('https://dummyjson.com/products/1') .then(res => res.json()) .then(console.log) .catch(err => console.error('Error:', err)); 3. Cari Produk Copy fetch('https://dummyjson.com/products/search?q=phone') .then(res => res.json()) .then(console.log) .catch(err => console.error('Error:', err)); 4. Tambah Produk Baru Copy fetch('https://dummyjson.com/products/add', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ title: 'BMW Pencil' }) }) .then(res => res.json()) .then(console.log) .catch(err => console.error('Error:', err)); 5. Update Produk Copy fetch('https://dummyjson.com/products/1', { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ title: 'iPhone Galaxy +1' }) }) .then(res => res.json()) .then(console.log) .catch(err => console.error('Error:', err)); 6. Hapus Produk Copy fetch('https://dummyjson.com/products/1', { method: 'DELETE' }) .then(res => res.json()) .then(console.log) .catch(err => console.error('Error:', err)); 7. Tips & Best Practices Error Handling: selalu pakai .catch() atau try/catch. Async/Await: lebih readable untuk kode kompleks: Copy async function fetchProducts() { try { const res = await fetch('https://dummyjson.com/products'); const data = await res.json(); console.log(data); } catch (err) { console.error('Error:', err); } } fetchProducts(); Keamanan: jangan lupa handle data sensitif dengan aman. Kesimpulan Fetch API bikin HTTP request mudah dan fleksibel di JavaScript. CRUD, searching, dan data manipulation jadi simpel tanpa ribet.
chevron-up