JavaScript: Başlangıçtan İleri Seviyeye
Bölüm 28 / 313 dk okuma

JSON ve Date

JSON serileştirme/parse ve modern tarih işlemleri.

JSON

JSON.stringify ve JSON.parse — JavaScript'in günlük ekmeği.

Stringify

JSON.stringify({ name: "F", age: 28 });
// '{"name":"F","age":28}'
 
// Pretty print
JSON.stringify(obj, null, 2);

Parse

const obj = JSON.parse('{"name":"F"}');

Geçersiz JSON → SyntaxError. Try/catch ile sar:

function parseSafe(text, fallback = null) {
  try { return JSON.parse(text); }
  catch { return fallback; }
}

Replacer / reviver

İkinci argüman ile dönüşümü özelleştir:

// Belirli alanları gizle
JSON.stringify(user, (key, value) => {
  if (key === "password") return undefined;
  return value;
});
 
// Parse sırasında string'i Date'e çevir
JSON.parse(text, (key, value) => {
  if (key === "createdAt") return new Date(value);
  return value;
});

toJSON metodu

Bir objede toJSON varsa stringify onu kullanır:

class User {
  constructor(name, password) {
    this.name = name;
    this.password = password;
  }
  toJSON() {
    return { name: this.name }; // password gizli
  }
}
 
JSON.stringify(new User("F", "secret"));
// '{"name":"F"}'

Sınırlamalar

  • undefined, fonksiyon, Symbol → atılır veya null olur
  • Circular reference → hata (TypeError)
  • BigInt → hata
  • Map, Set, Date → düzgün çevrilmez (Date string olur)

Date

JavaScript'in tarih objesi. Eski API, biraz garip ama temellerini bilmek şart.

Oluşturma

new Date();                          // şimdi
new Date("2025-12-25");              // ISO string
new Date(2025, 11, 25);              // ay 0-indexli! 11 = Aralık
new Date(2025, 11, 25, 14, 30);      // yıl ay gün saat dakika
new Date(1704067200000);             // unix ms

Okuma

const d = new Date();
 
d.getFullYear();      // 2026
d.getMonth();         // 0-11
d.getDate();          // 1-31
d.getDay();           // 0-6 (Pazar=0)
d.getHours();
d.getMinutes();
d.getSeconds();
d.getTime();          // unix ms — sayı
 
Date.now();           // şimdiki unix ms (Date oluşturmadan)

Format

const d = new Date();
 
d.toISOString();       // "2026-04-27T10:30:00.000Z"
d.toLocaleDateString("tr-TR");
// "27.04.2026"
 
d.toLocaleString("tr-TR", {
  weekday: "long",
  year: "numeric",
  month: "long",
  day: "numeric",
});
// "27 Nisan 2026 Pazartesi"
 
new Intl.DateTimeFormat("tr-TR", {
  dateStyle: "medium",
  timeStyle: "short",
}).format(d);
// "27 Nis 2026 13:30"

Karşılaştırma ve aritmetik

const a = new Date("2025-01-01");
const b = new Date("2025-12-31");
 
a < b;                  // true
b - a;                  // ms farkı
(b - a) / 1000 / 60 / 60 / 24; // gün
 
// Yarın
const yarin = new Date();
yarin.setDate(yarin.getDate() + 1);

Yaygın bug: ISO ve timezone

new Date("2025-12-25").toString();
// Tarayıcının timezone'una göre farklı görünebilir!

UTC ve local saat karışır. Çözüm: kütüphane kullan (date-fns, Day.js) veya yeni Temporal API'sini bekle.

💡İpucu

Veritabanına ve API'ye her zaman ISO 8601 / UTC yaz (d.toISOString()). Tarihi sadece kullanıcıya gösterirken Intl.DateTimeFormat ile lokalize et.

Intl — uluslararasılaştırma

Sayılar, para birimi, listeler için:

new Intl.NumberFormat("tr-TR").format(1234567.89);
// "1.234.567,89"
 
new Intl.NumberFormat("tr-TR", {
  style: "currency",
  currency: "TRY",
}).format(1234.5);
// "₺1.234,50"
 
new Intl.RelativeTimeFormat("tr-TR").format(-3, "day");
// "3 gün önce"

Çoğunlukla kütüphaneye gerek bırakmaz.

Bu bölümü bitirdin mi?

İlerlemen tarayıcıda saklanır, eğitim listesinde görünür.

Paylaş