v2.5.2
Giriş yap

Daha iyi bir yöntem var mı ?

mlhslckr
239 defa görüntülendi

Data baseden "2019-01-01T00:00:00" bu şekilde gelen veriyi JS kullanarak gg/aa/yyyy şeklinde yazmak istiyorum slice() metodunu mu kullanmayalım yoksa daha iyi bir yöntem var mı ?

{gelir.tarih.slice(8, 10)}-{gelir.tarih.slice(5, 7)}-{gelir.tarih.slice(0, 4)}

Çıktı:
15-02-2022

Bu şekilde.

Cevap yaz
Cevaplar (2)
devepdogukan
426 gün önce

Bu daha fazla işini görecektir.

function formatDate(dateTime,seperator="-"){
  const _date = new Date(dateTime);
  const day = _date.getDate().toString().padStart(2,0);
  const month = (_date.getMonth() + 1).toString().padStart(2,0);
  const year = _date.getFullYear();
  return [day,month,year].join(seperator)
}

console.log(formatDate("2019-01-15T00:00:00","/")) //Çıktı: 15/01/2019
console.log(formatDate("2019-01-22T00:00:00")) //Çıktı: 22-01-2019
istek61
427 gün önce

function oluşturup çok kolay kullanılabilir.

const today = new Date("2019-01-01T00:00:00");
const yyyy = today.getFullYear();
let mm = today.getMonth() + 1; 
let dd = today.getDate();

if (dd < 10) dd = '0' + dd;
if (mm < 10) mm = '0' + mm;

const formattedToday = dd + '/' + mm + '/' + yyyy;

console.log(formattedToday);