জাভাস্ক্রিপ্টে ডেট ফরম্যাট ইমপ্লিমেন্ট করার কোড নিচে দেওয়া হলো।
উদাহরণ
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; }
.sample { font-size: 18px; font-weight: 500; }
</style>
</head>
<body>
<h1>Date Format</h1>
<div class="sample"></div>
<button class="Btn">CLICK HERE</button>
<h3>Click on the above button to see the different date formats</h3>
<script>
let fillEle = document.querySelector(".sample");
let date = [];
date.push(new Date('2020-01-20'));
date.push(new Date('2020-02'));
date.push(new Date('2020'));
date.push(new Date("2015-03-25T12:00:00Z"));
fillEle.innerHTML = date + '<br>';
document.querySelector(".Btn").addEventListener("click", () => {
date.forEach(item => {
fillEle.innerHTML += item + '<br>';
});
});
</script>
</body> </html>
আউটপুট (ব্রাউজার টাইমজোন অনুযায়ী ভিন্ন হতে পারে)
Mon Jan 20 2020 06:00:00 GMT+0600 (Bangladesh Standard Time) Sat Feb 01 2020 06:00:00 GMT+0600 (Bangladesh Standard Time) Wed Jan 01 2020 06:00:00 GMT+0600 (Bangladesh Standard Time) Wed Mar 25 2015 18:00:00 GMT+0600 (Bangladesh Standard Time)
ISO 8601 ডেট স্ট্রিং ফরম্যাট
| ফরম্যাট | উদাহরণ | ব্যাখ্যা |
|---|---|---|
| YYYY-MM-DD | 2020-01-20 | তاریখ মাত্র |
| YYYY-MM | 2020-02 | বছর-মাস (দিন ১ ধরে) |
| YYYY | 2020 | শুধু বছর (Jan 1 ধরে) |
| YYYY-MM-DDTHH:mm:ssZ | 2015-03-25T12:00:00Z | UTC টাইম (Z = জুলু টাইম) |
| YYYY-MM-DDTHH:mm:ss±hh:mm | 2020-01-20T10:30:00+06:00 | টাইমজোন অফসেটসহ |
মডার্ন ফরম্যাটিং: Intl.DateTimeFormat
const date = new Date('2020-01-20T10:30:00Z');
console.log(new Intl.DateTimeFormat('bn-BD', {
dateStyle: 'full', timeStyle: 'short'
}).format(date));
// "শনিবার, ২০ জানুয়ারি, ২০২০, অপরাহ্ন ৪:৩০"
console.log(new Intl.DateTimeFormat('en-US', {
year: 'numeric', month: 'long', day: 'numeric'
}).format(date));
// "January 20, 2020"
Intl.DateTimeFormat লোকেল-অ্যোয়ার ফরম্যাটিং দেয়, যা প্রোডাকশন-রেডি।