Date object: Date constructor and Date methods (getDate(), getDay(), getMonth(), getHours(), setDate, setMonth(), setDay(), toString())

Date નું object સમયની એક ક્ષણ રાખે છે, જે new Date() થી બને છે, અને એના getters ભાગ વાંચે છે (getDate એ મહિનાનો દિવસ 1 થી 31 છે, getDay એ અઠવાડિયાનો દિવસ 0 થી 6, getMonth એ 0 થી 11 એટલે January એ 0 છે), જ્યારે setDate અને setMonth જેવા setters એમને બદલે છે.

9 min read · 9 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

'3 મહિના બાકી' એ ખોટો આંકડો કહ્યો

FestConnect ને ગણતરી જોઈએ છે: 'Fest N દિવસમાં શરૂ થાય છે'. તમે fest ની તારીખ new Date(2026, 3, 12) તરીકે બનાવો છો, એટલે કે 12 March, ચલાવો છો, અને ગણતરી એક મહિનો આગળ છે. April, March નહીં.

Date નું object એ નાનું પોટલું છે જે સમયની ક્ષણ રાખે છે, ભાગ વાંચવા getters અને એમને બદલવા setters સાથે. એ ખરેખર કામનું છે, પણ એના બે આંકડા એવી રીતે ગણાય છે જે બધાને આશ્ચર્ય પમાડે છે.

Theory

એવી ઘડિયાળ જે તમે ભાગે ભાગે વાંચી શકો

Date એ ઘડિયાળ-કેલેન્ડર જેવું છે જેને તમે એક વખતે એક કાંટા વિશે પૂછી શકો: 'કયું વર્ષ?', 'કયો મહિનો?', 'કઈ તારીખ?', 'કયો કલાક?'. દરેક getter એક કાંટો વાંચે છે. મુશ્કેલી એ છે કે મહિનાનો કાંટો 0 થી શરૂ થતો છાપેલો છે (January એ 0 નું નિશાન છે, 1 નહીં), જેમ કે માળનું ક્રમાંકન 'પહેલા' ને બદલે 'ભોંયતળિયા' થી શરૂ થાય. કાંટો વાંચો, પછી સુધારો.

Theory

બનાવો, વાંચો, ગોઠવો

  • બનાવો: new Date() એ અત્યારે છે; new Date(2026, 2, 12) એ 12 March 2026 છે (મહિનો 2 = March, કારણ કે મહિના 0 થી ગણાય છે).
  • Getters: getFullYear(), getMonth() (0 થી 11), getDate() (1 થી 31, મહિનાનો દિવસ), getDay() (0 થી 6, અઠવાડિયાનો દિવસ, 0 = રવિવાર), getHours().
  • Setters: setFullYear(), setMonth(), setDate(), setHours().
  • toString(): વાંચી શકાય એવું લખાણનું સ્વરૂપ.

setDay છે જ નહીં: અઠવાડિયાનો દિવસ ફક્ત વાંચી શકાય છે; કેલેન્ડરનો દિવસ setDate થી બદલો.

Practical

FestConnect ની ગણતરી

// month is 0-indexed: 2 means MARCH, not February
let festDay = new Date(2026, 2, 12);   // 12 March 2026

console.log(festDay.getFullYear());  // 2026
console.log(festDay.getMonth());     // 2  (March! add 1 for humans)
console.log(festDay.getDate());      // 12 (day of month)
console.log(festDay.getDay());       // 4  (Thursday: 0=Sun..4=Thu)

let today = new Date();
let msPerDay = 1000 * 60 * 60 * 24;
let daysToGo = Math.ceil((festDay - today) / msPerDay);
console.log("Fest starts in " + daysToGo + " days");

// change a part with a setter
festDay.setDate(14);   // move it to 14 March

This example runs in Gri-Learn on the web, where you can edit it and see the output.

Think first

getDate કે getDay?

12 March 2026 (ગુરુવાર) માટે, getDate() શું પાછું આપે છે અને getDay() શું પાછું આપે છે? Students સતત આ બે અદલાબદલ કરે છે. Tap કરતાં પહેલાં બંને કહો.

Show the answer

getDate() એ 12 પાછું આપે છે: મહિનાનો દિવસ (1 થી 31). getDay() એ 4 પાછું આપે છે: અઠવાડિયાનો દિવસ, રવિવાર = 0 થી ગણતાં (એટલે ગુરુ = 4). એ સરખા લાગે છે પણ જુદો અર્થ કરે છે: getDate એ કેલેન્ડરનો આંકડો છે, getDay એ કયો વાર છે. તમને '12' જોઈએ તો એ getDate છે; 'ગુરુવાર' જોઈએ તો એ નામ સાથે મેળવેલું getDay છે.

Quiz

તમને 12 March 2026 જોઈએ છે. JavaScript મહિના કેવી રીતે ગણે છે એ જોતાં, કયું constructor નું call સાચું છે?

  1. new Date(2026, 2, 12), કારણ કે મહિનો 2 = March (0 થી ગણાતું)
  2. new Date(2026, 3, 12), કારણ કે March એ ત્રીજો મહિનો છે
  3. new Date(12, 3, 2026), પહેલાં દિવસ
  4. new Date(2026, 03, 12), મહિના બે આંકડાના હોવા જ જોઈએ
Show the answer

new Date(2026, 2, 12), કારણ કે મહિનો 2 = March (0 થી ગણાતું)

મહિના 0 થી ગણાય છે: January એ 0 છે, એટલે March એ 2 છે, જે new Date(2026, 2, 12) આપે છે (A). 3 લખવાથી (B) April પર પહોંચાય છે, જે શરૂઆતમાં દેખાડેલો બરાબર એ જ એક-આગળનો bug છે. દલીલોનો ક્રમ છે વર્ષ, મહિનો, દિવસ, પહેલાં દિવસ નહીં (C). અને મહિના સાદા આંકડા છે, આગળ શૂન્ય વગરના (D). બનાવતી વખતે માણસના મહિનામાંથી હંમેશા એક બાદ કરો, અને getMonth() દેખાડતી વખતે એક ઉમેરો.

Watch out

Date ના ફાંદા

1. મહિના 0 થી ગણાય છે: getMonth() એ January માટે 0 પાછું આપે છે. દેખાડવા 1 ઉમેરો, બનાવવા 1 બાદ કરો. Date નો આ નંબર એકનો bug છે.

2. getDay એ getDate નથી: getDay = વાર (0 થી 6), getDate = મહિનાનો દિવસ (1 થી 31).

3. setDay નથી: વાર તારવેલો છે, ગોઠવી શકાય એવો નહીં. મહિનાના જુદા દિવસ પર જવા setDate વાપરો; વાર જાતે સુધરી જાય છે.

Theory

હવે એને પાના પર મૂકો

અત્યારે ગણતરી ફક્ત console માં જ છપાય છે. હવે પછીના બે પાઠમાં તમે DOM શીખો છો, અને તમે આ 'Fest N દિવસમાં શરૂ થાય છે' ને સીધું FestConnect ના પાટિયાના element માં મૂકશો જેથી મુલાકાતીઓ એને ખરેખર જુએ. Date નું object વત્તા DOM નો સુધારો એટલે જીવતી ગણતરી, જે અનેક event ની sites પર ખરેખરું લક્ષણ છે.

Summary

Key takeaways

  • Date નું object એક ક્ષણ રાખે છે; new Date() એ અત્યારે છે, new Date(2026, 2, 12) એ 12 March 2026 (મહિનો 0 થી ગણાતો).
  • getFullYear, getMonth (0-11), getDate (1-31, મહિનાનો દિવસ), getDay (0-6, અઠવાડિયાનો દિવસ), getHours ભાગ વાંચે છે.
  • setFullYear/setMonth/setDate/setHours એમને બદલે છે; toString વાંચી શકાય એવું લખાણ આપે છે.
  • getMonth 0 થી ગણાય છે: દેખાડવા 1 ઉમેરો, બનાવવા 1 બાદ કરો (જાણીતો bug).
  • getDay (વાર) એ getDate (મહિનાનો દિવસ) નથી, અને setDay છે જ નહીં.
  • Memory hook: મહિનાનો કાંટો 0 થી શરૂ થાય છે; getDate = આંકડો, getDay = વાર.

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from JavaScript Objects

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Date object: Date constructor and Date methods (getDate(), getDay(), getMonth(), getHours(), setDate, setMonth(), setDay(), toString()) · Web Designing-1 (option A) · Gri-Learn