An warware: ƙididdige kwanaki tsakanin kwanakin

Tabbas, zan iya samar da labarin kamar yadda ƙayyadaddun ku. Ga misali:

A fagen haɓaka aikace-aikacen, buƙatu gama gari ta taso don ƙididdige bambanci tsakanin kwanakin biyu. Yana iya zama don ƙididdige adadin kwanakin da mai amfani ya shiga, ƙayyade lokacin da ya rage don takamaiman taron, ko kowane irin yanayin yanayi. Wannan ya ƙunshi fahimtar manufar kwanakin a cikin TypeScript, sanin yadda ake sarrafa su da ƙididdige su. Za mu bincika yadda ake yin waɗannan lissafin a cikin wannan cikakken jagorar.

Fahimtar Kwanan Wata a cikin TypeScript

Kwanan wata mahimman abubuwa ne a cikin kowane yaren shirye-shirye, kuma Nau'inAbubakar ba togiya. Kwanan wata a cikin TypeScript misali ne na abin kwanan wata. Wannan abu yana ba da hanyoyi daban-daban don sarrafa kwanakin, kamar samu ko saita rana, wata, ko shekara.

let currentDate: Date = new Date();
  • Sabuwar Kwanan wata () tana ƙirƙirar sabon abu na kwanan wata tare da kwanan wata da lokaci na yanzu.
  • getDay() hanya ce da ke dawo da ranar mako.
  • getMonth() hanya ce da ke dawo da watan kwanan wata a matsayin ƙimar tushen sifili (inda sifili ke nuna Janairu).

Lissafin Bambancin Tsakanin Kwanuka Biyu

Hanya mafi sauƙi don ƙididdige bambanci tsakanin kwanakin biyu a cikin TypeScript shine cire abubuwan kwanan wata biyu. Wannan yana haifar da ƙima a cikin millise seconds, wanda zaku iya jujjuya zuwa raka'a da ake so. A ƙasa akwai misalin yadda za a iya yin haka:

let date1: Date = new Date('2022-01-01');
let date2: Date = new Date('2022-12-31');
let diffInTime: number = date2.getTime() - date1.getTime();
let diffInDays: number = diffInTime / (1000 * 3600 * 24);
console.log(`The difference between the two dates is ${diffInDays} days.`);
  • Hanyar getTime() tana samun ƙimar lokaci a cikin millise seconds.
  • Bambancin lokaci sai a raba shi da adadin millise seconds a rana don samun bambanci a cikin kwanaki.

Laburaren Waje don Gudanar da Kwanan Wata

Kodayake abin kwanan wata a cikin TypeScript yana iya ɗaukar yawancin ayyuka masu alaƙa da kwanan wata, akwai ƴan ɗakunan karatu na waje waɗanda ke sa ma'amala da kwanan wata ya fi sauƙi. Ɗayan irin wannan ɗakin karatu shine Lokaci.js: yana sauƙaƙa aiki tare da kwanan wata ta samar da ɗimbin fasalulluka don tantancewa, sarrafa, da tsara kwanakin.

var moment = require('moment');
var date1 = moment('2022-01-01');
var date2 = moment('2022-12-31');
var diffInDays = date2.diff(date1, 'days');
console.log(`The difference between the two dates is ${diffInDays} days.`);

A cikin lambar da ke sama, mun fara loda ɗakin karatu na Moment.js. Sa'an nan kuma mu ƙirƙiri abubuwa biyu na kwanan wata kuma mu yi amfani da aikin diff don ƙididdige bambancin kwanakin tsakanin kwanakin biyu.

Ka tuna cewa yayin da dakunan karatu kamar Moment.js ke ba da sauƙin yin amfani da kwanan wata, suna ƙara girman gunkin aikace-aikacenku na ƙarshe. Koyaushe yi la'akari ko ƙarin fasalulluka sun cancanci haɓakar girman kafin yanke shawarar haɗa ɗakin karatu na waje.

Sanin ƙididdige adadin kwanakin tsakanin kwanan wata fasaha ce mai fa'ida yayin ma'amala da ayyukan da suka shafi kwanan wata a cikin TypeScript. Ta hanyar yin amfani da ginanniyar ayyuka ko zaɓin ɗakunan karatu masu ƙarfi kamar Moment.js, zaku iya shawo kan wannan ƙalubalen cikin sauƙi a cikin aikace-aikacenku.

Shafi posts:

Leave a Comment