Date တွေ အစီအစဉ်ကျဖို့ ဒီ code လေးက အသုံး၀င်ပါလိမ့်မယ် 👀
=°=°=°=°=°=°=°=°=°=°=°=°=°=°=°=°=°=°=°=°=°=°=°
ဒီအပတ်မှာတော့ Date Range Array function လေးအကြောင်းပြောပြပေးသွားပါမယ်။
Code example လေးနဲ့အတူ ကြည့်လိုက်ရအောင် 👀
function dateRangeArray(start, end, step = 1, unit = 'days') {
// Validate input
if (!(start instanceof Date) !(end instanceof Date)) {
throw new TypeError('Start and end must be Date objects.');
}
if (start > end) {
throw new RangeError('Start date must be before end date.');
}
if (typeof step !== 'number' step <= 0) {
throw new RangeError('Step must be a positive number.');
}
if (!['days', 'months', 'years'].includes(unit)) {
throw new RangeError('Unit must be "days", "months", or "years".');
}
const dates = [];
let current = new Date(start); // Create a new date instance to avoid mutation of the original date
while (current < end) {
dates.push(new Date(current)); // Add a copy of the current date to the array
// Increment the date based on the specified unit
switch (unit) {
case 'days':
current.setDate(current.getDate() + step);
break;
case 'months':
current.setMonth(current.getMonth() + step);
break;
case 'years':
current.setFullYear(current.getFullYear() + step);
break;
}
}
return dates;
}
//usage example
const start = new Date('2024-07-01');
const end = new Date('2024-12-01');
const step = 2;
const unit = 'months';
const dates = dateRangeArray(start, end, step, unit);
function dateRangeArray(start, end, step = 1, unit = 'days')
ပထမဆုံး အနေနဲ့ dateRangeArray ဆိုတဲ့ function တစ်ခုကြေငြာပြီး argument တွေအနေနဲ့ start and end ဆိုတဲ့ date range ၂ခုကို လက်ခံပြီး step ကတော့ date range အတွက် step သက်မှက်ပေးဖို့ဖြစ်ပြီး default အနေနဲ့ ၁ ကို ပေးထားပါတယ်။
unit ဆိုတာကတော့ date range မှာဘယ်ဟာကို range အနေနဲ့ သတ်မှတ်မလဲဆိုတာကိုပြော တာဖြစ်ပါတယ်။
ဆိုလိုတာက days ,months, years ဖြစ်ပြီး default အနေနဲ့ days ကိုပေးထားပါတယ်
if (!(start instanceof Date) !(end instanceof Date)) {
throw new TypeError('Start and end must be Date objects.');
}
ဒီဟာကတော့ validation စစ်တဲ့ code ဖြစ်ပြီး user က အနည်းဆုံး start and end date ကို date object အနေနဲ့ ပေးမပေးစစ်မှာဖြစ်ပြီး date object မဟုတ်တဲ့ string တွေ (eg. ("24.12.2024)) ဒီလိုမျိုးဆို error ပြန်ပေးမှာဖြစ်ပါတယ်။
if (typeof step !== 'number' step <= 0) {
throw new RangeError('Step must be a positive number.');
}
ဒီဟာလည်း အပေါ်ကနည်းတူ validation စစ်ပီး end date က start date စောမစောစစ်ပြီး end date က start date ထက်စောနေရင် error ပြန်ပါတယ်
if (!['days', 'months', 'years'].includes(unit)) {
throw new RangeError('Unit must be "days", "months", or "years".');
}
unit မှာ ပေးတဲ့ data ကို validation စစ်တာ ဖြစ်ပြီး days,months or years မဟုတ်ပဲ တစ်ခြားစာတွေထည့်ရင် error ပြန်ပေတာဖြစ်ပါတယ်
const dates = [];
ဒါက dates ကို empty array ပေးလိုက်ပြီး variable ထဲမှာသိမ်းလိုက်ပါတယ်။
let current = new Date(start);
date range iteration လုပ်နေချိန် start date ကို mutation မဖြစ်အောင် current ဆိုတဲ့ variable ထဲမှာ new Date(start) ဆိုပြီး သိမ်းထားလိုက်ပါတယ်။
while (current < end) {
dates.push(new Date(current));
while loop နဲ့ current date က end date ထက်နည်းနေသရွေ့ dates array ထဲမှာ ထည့်ထည့်ပြီးသိမ်းထားလိုက်ပါတယ်။
switch (unit) {
case 'days':
current.setDate(current.getDate() + step);
break;
case 'months':
current.setMonth(current.getMonth() + step);
break;
case 'years':
current.setFullYear(current.getFullYear() + step);
break;
}
switch case နဲ့ unit မှာသတ်မှတ်ထားတဲ days ,months or years ကို step နဲ့ ပေါင်းပြီး curent date object ကို modify လိုက်ပါတယ်
return dates; ပြီးတော့ရလာတဲ့ date ကို return ပေးလိုက်ပါတယ်။
ဒါကတော့ date range array လေးရဲ့အသုံး၀င်ပုံလေးဖြစ်ပါတယ်။
အခြား developer တွေသိသွားအောင်လည်း share ပေးဖို့ မမေ့ကြနဲ့နော် 👀
#creative_coder_myanmar
=°=°=°=°=°=°=°=°=°=°=°=°=°=°=°=°=°=°=°=°=°=°=°
ဒီအပတ်မှာတော့ Date Range Array function လေးအကြောင်းပြောပြပေးသွားပါမယ်။
Code example လေးနဲ့အတူ ကြည့်လိုက်ရအောင် 👀
function dateRangeArray(start, end, step = 1, unit = 'days') {
// Validate input
if (!(start instanceof Date) !(end instanceof Date)) {
throw new TypeError('Start and end must be Date objects.');
}
if (start > end) {
throw new RangeError('Start date must be before end date.');
}
if (typeof step !== 'number' step <= 0) {
throw new RangeError('Step must be a positive number.');
}
if (!['days', 'months', 'years'].includes(unit)) {
throw new RangeError('Unit must be "days", "months", or "years".');
}
const dates = [];
let current = new Date(start); // Create a new date instance to avoid mutation of the original date
while (current < end) {
dates.push(new Date(current)); // Add a copy of the current date to the array
// Increment the date based on the specified unit
switch (unit) {
case 'days':
current.setDate(current.getDate() + step);
break;
case 'months':
current.setMonth(current.getMonth() + step);
break;
case 'years':
current.setFullYear(current.getFullYear() + step);
break;
}
}
return dates;
}
//usage example
const start = new Date('2024-07-01');
const end = new Date('2024-12-01');
const step = 2;
const unit = 'months';
const dates = dateRangeArray(start, end, step, unit);
function dateRangeArray(start, end, step = 1, unit = 'days')
ပထမဆုံး အနေနဲ့ dateRangeArray ဆိုတဲ့ function တစ်ခုကြေငြာပြီး argument တွေအနေနဲ့ start and end ဆိုတဲ့ date range ၂ခုကို လက်ခံပြီး step ကတော့ date range အတွက် step သက်မှက်ပေးဖို့ဖြစ်ပြီး default အနေနဲ့ ၁ ကို ပေးထားပါတယ်။
unit ဆိုတာကတော့ date range မှာဘယ်ဟာကို range အနေနဲ့ သတ်မှတ်မလဲဆိုတာကိုပြော တာဖြစ်ပါတယ်။
ဆိုလိုတာက days ,months, years ဖြစ်ပြီး default အနေနဲ့ days ကိုပေးထားပါတယ်
if (!(start instanceof Date) !(end instanceof Date)) {
throw new TypeError('Start and end must be Date objects.');
}
ဒီဟာကတော့ validation စစ်တဲ့ code ဖြစ်ပြီး user က အနည်းဆုံး start and end date ကို date object အနေနဲ့ ပေးမပေးစစ်မှာဖြစ်ပြီး date object မဟုတ်တဲ့ string တွေ (eg. ("24.12.2024)) ဒီလိုမျိုးဆို error ပြန်ပေးမှာဖြစ်ပါတယ်။
if (typeof step !== 'number' step <= 0) {
throw new RangeError('Step must be a positive number.');
}
ဒီဟာလည်း အပေါ်ကနည်းတူ validation စစ်ပီး end date က start date စောမစောစစ်ပြီး end date က start date ထက်စောနေရင် error ပြန်ပါတယ်
if (!['days', 'months', 'years'].includes(unit)) {
throw new RangeError('Unit must be "days", "months", or "years".');
}
unit မှာ ပေးတဲ့ data ကို validation စစ်တာ ဖြစ်ပြီး days,months or years မဟုတ်ပဲ တစ်ခြားစာတွေထည့်ရင် error ပြန်ပေတာဖြစ်ပါတယ်
const dates = [];
ဒါက dates ကို empty array ပေးလိုက်ပြီး variable ထဲမှာသိမ်းလိုက်ပါတယ်။
let current = new Date(start);
date range iteration လုပ်နေချိန် start date ကို mutation မဖြစ်အောင် current ဆိုတဲ့ variable ထဲမှာ new Date(start) ဆိုပြီး သိမ်းထားလိုက်ပါတယ်။
while (current < end) {
dates.push(new Date(current));
while loop နဲ့ current date က end date ထက်နည်းနေသရွေ့ dates array ထဲမှာ ထည့်ထည့်ပြီးသိမ်းထားလိုက်ပါတယ်။
switch (unit) {
case 'days':
current.setDate(current.getDate() + step);
break;
case 'months':
current.setMonth(current.getMonth() + step);
break;
case 'years':
current.setFullYear(current.getFullYear() + step);
break;
}
switch case နဲ့ unit မှာသတ်မှတ်ထားတဲ days ,months or years ကို step နဲ့ ပေါင်းပြီး curent date object ကို modify လိုက်ပါတယ်
return dates; ပြီးတော့ရလာတဲ့ date ကို return ပေးလိုက်ပါတယ်။
ဒါကတော့ date range array လေးရဲ့အသုံး၀င်ပုံလေးဖြစ်ပါတယ်။
အခြား developer တွေသိသွားအောင်လည်း share ပေးဖို့ မမေ့ကြနဲ့နော် 👀
#creative_coder_myanmar
Developer Bundle Course Package Promotion လေးဟာ ကျောင်းသား ၄ ယောက်သာ လိုတာမို့နောက်မကျစေနဲ့ဦးနော်ဗျို့။🚀
- ဆရာနဲ့ တစ်ယောက်ချင်း Video Meeting ခေါ်စာမေးနိုင်ခြင်း
- Website Live Chat ကနေ တခုခု အခက်ခဲရှိလာတာနဲ့ မေးမြန်းနိုင်ခြင်း
- Lifetime Update တစ်သက်တာ ပိုင်ဆိုင်နိုင်မှာဖြစ်တဲ့ အပြင် အပေါ်က service တွေလဲ lifetime ရရှိမှာဖြစ်တာမို့ သေချာဖြုတ်မဲ့သူတွေအတွက် အကောင်းဆုံး investment ဖြစ်မယ်လို့ယုံကြည်ပါတယ်ခင်ဗျာ။💯
- ဆရာနဲ့ တစ်ယောက်ချင်း Video Meeting ခေါ်စာမေးနိုင်ခြင်း
- Website Live Chat ကနေ တခုခု အခက်ခဲရှိလာတာနဲ့ မေးမြန်းနိုင်ခြင်း
- Lifetime Update တစ်သက်တာ ပိုင်ဆိုင်နိုင်မှာဖြစ်တဲ့ အပြင် အပေါ်က service တွေလဲ lifetime ရရှိမှာဖြစ်တာမို့ သေချာဖြုတ်မဲ့သူတွေအတွက် အကောင်းဆုံး investment ဖြစ်မယ်လို့ယုံကြည်ပါတယ်ခင်ဗျာ။💯
Learning Platform မှာ သင်ကြားရတဲ့အတွေ့အကြုံတွေကို ပြောပြထားတဲ့ ကျောင်းသားတို့ရဲ့ review များ 📝
Website ကနေတဆင့်စာသင်ကြားရတဲ့ အတွေ့အကြုံတွေကို Creative Coder ရဲ့ကျောင်းသားတွေကတော့ အခုလိုပဲ ပြောပြပေးခဲ့ပါတယ်။
အခုလက်ရှိမှာလည်း နှစ်သိန်းအထိ လျှော့ပေးထားတဲ့ဈေးနဲ့ learning platform အတန်းတွေအားလုံးပါ၀င်တဲ့ package ကို တက်ရောက်နိုင်မှာဖြစ်လို့ page messenger ကနေသင်တန်းအပ်လို့ရပြီနော် 👀
Website ကနေတဆင့်စာသင်ကြားရတဲ့ အတွေ့အကြုံတွေကို Creative Coder ရဲ့ကျောင်းသားတွေကတော့ အခုလိုပဲ ပြောပြပေးခဲ့ပါတယ်။
အခုလက်ရှိမှာလည်း နှစ်သိန်းအထိ လျှော့ပေးထားတဲ့ဈေးနဲ့ learning platform အတန်းတွေအားလုံးပါ၀င်တဲ့ package ကို တက်ရောက်နိုင်မှာဖြစ်လို့ page messenger ကနေသင်တန်းအပ်လို့ရပြီနော် 👀