Skip to content

Latest commit

 

History

History
224 lines (156 loc) · 11.1 KB

26-date.md

File metadata and controls

224 lines (156 loc) · 11.1 KB

Creating Dates and different ways of creating Date object

Date object ek built-in object hai jo JavaScript mein available hota hai aur isse hum date aur time ko represent kar sakte hain. Date object ke use se hum current date, specific date ya phir time ki calculations kar sakte hain.

JavaScript mein Date object ko create karne ke kuch different tarike hain:

  1. Current date ka use karke: Hum simply Date() function ka use karke current date and time ko generate kar sakte hain.

Example:

const currentDate = new Date();
console.log(currentDate); // output: Mon Apr 05 2023 16:23:34 GMT+0530 (India Standard Time)
  1. Specific date ka use karke: Agar humein kisi specific date ko JavaScript mein represent karna hai to hum String format mein date ko pass kar sakte hain.

Example:

const specificDate = new Date("2023-04-10");
console.log(specificDate); // output: Sun Apr 09 2023 18:30:00 GMT+0530 (India Standard Time)
  1. Number of milliseconds ka use karke: Hum Date object ko create kar sakte hain by passing the number of milliseconds since January 1, 1970, 00:00:00 UTC.

Example:

const dateInMs = new Date(1657209000000);
console.log(dateInMs); // output: Fri Jul 07 2022 02:30:00 GMT+0530 (India Standard Time)
  1. Specific year, month, day, hour, minute, second aur millisecond ka use karke: Hum Date object ko aise bhi create kar sakte hain jismein humein specific year, month, day, hour, minute, second aur millisecond ki values pass karni hoti hai.

Example:

const specificDateTime = new Date(2023, 3, 10, 11, 30, 0, 0);
console.log(specificDateTime); // output: Fri Apr 10 2023 11:30:00 GMT+0530 (India Standard Time)

Is tarah se hum JavaScript mein Date object create kar sakte hain aur apne kaam ke according uska use bhi kar sakte hain.

Understanding milliseconds and other units of time

Javascript me time ka istemal hum kabhi bhi kar sakte hai, lekin iske units ki samajh jaruri hai, jaise milliseconds. Milliseconds ek bahut chota unit hai, jo 1 second ke 1000 parts ko represent karta hai. Isi tarah, dusre units hai jaise seconds, minutes, hours, days.

Jab hamara program chalta hai to hume kabhi kabhi time tracking ki zarurat padti hai, jaise ek timer ya animation ke liye. In cases me hum milliseconds ka use karte hai. Javascript me time tracking ke liye hume Date object use karna hota hai.

Example: Agar aapko ek function banana hai jo 5 seconds ke baad kuchh execute kare, to iska code kuchh is tarah se hoga:

setTimeout(function () {
  console.log("Function executed after 5 seconds");
}, 5000);

setTimeout() function ka use kiya hai, jo ek callback function ke sath ek time interval deta hai. Time interval milliseconds me specify kiya jata hai. Is example me, humne setTimeout() me 5000 pass kiya hai, matlab ki 5 seconds (5000 milliseconds) ke baad callback function execute hoga.

Isi tarah, setInterval() function ka use bhi kiya ja sakta hai. setInterval() function callback function ko specified time interval ke baad repeated tarike se call karta hai. Example ke liye:

setInterval(function () {
  console.log("Function executed every 2 seconds");
}, 2000);

Is example me, setInterval() function 2000 milliseconds me callback function ko repeat tarike se call karega, matlab ki har 2 seconds baad.

Javascript me time units ki samajh bahut zaruri hai, kyunki isse hum animations aur timers ke liye sahi logic banane me madad milti hai.

what is Operations with Dates in javasript ? Explain in detail with example and in Hinglish language.

JavaScript provides built-in support for performing operations with dates. You can create, manipulate, and format dates using Date objects and various methods available in JavaScript's Date API.

To create a new date object, you can use the new Date() constructor. For example, to create a date object representing the current date and time, you can simply call new Date().

const today = new Date();

You can also create a date object by passing in a string representing the desired date and time. The string should be formatted in a specific way, such as "YYYY-MM-DDTHH:mm:ss.sssZ". For example:

const christmas = new Date('2022-12-25T00:00:00.000Z');

Once you have a date object, you can perform various operations on it. For example, you can get the year, month, day, hour, minute, and second of a date using various getter methods.

const year = today.getFullYear(); // returns the current year
const month = today.getMonth(); // returns the current month (0-11)
const day = today.getDate(); // returns the current day of the month (1-31)
const hour = today.getHours(); // returns the current hour (0-23)
const minute = today.getMinutes(); // returns the current minute (0-59)
const second = today.getSeconds(); // returns the current second (0-59)

You can also set the year, month, day, hour, minute, and second of a date using various setter methods.

today.setFullYear(2023); // sets the year to 2023
today.setMonth(3); // sets the month to April (0-based)
today.setDate(5); // sets the day of the month to 5
today.setHours(12); // sets the hour to 12
today.setMinutes(30); // sets the minute to 30
today.setSeconds(0); // sets the second to 0

You can perform arithmetic operations on dates using various methods, such as getTime(), which returns the number of milliseconds since January 1, 1970.

const christmas = new Date('2022-12-25T00:00:00.000Z');
const now = new Date();
const differenceInMilliseconds = christmas.getTime() - now.getTime();
const differenceInDays = differenceInMilliseconds / (1000 * 60 * 60 * 24);

Finally, you can format a date object into a string using various methods, such as toLocaleDateString() or toLocaleTimeString(). For example:

const formattedDate = today.toLocaleDateString('hi-IN'); // formats the date as "dd/MM/yyyy"
const formattedTime = today.toLocaleTimeString('hi-IN'); // formats the time as "hh:mm:ss AM/PM"

Yadi aap kisi specific date ko represent karna chahte hai toh aap 'new Date()' constructor ka use kar sakte hai. Jaise ki humne upar diya hai. Iske baad aap date ke year, month, day, hour, minute aur second ka pata laga sakte hai getters ka use karke. Iske alawa aap setters ka use karke bhi ye values set kar sakte hai. Aap getTime() method ka use karke dates ke beech me arithmetic operations kar sakte hai. Finally, aap dates ko string me convert karke display kar sakte hai by using toLocaleDateString() aur toLocaleTimeString() methods.

what is Date setter methods

Date setter methods in JavaScript are functions that allow you to set specific parts of a Date object, such as the year, month, day, hour, minute, and second.

For example, let's say you want to create a Date object for April 5th, 2023 at 9:30 AM. You can use the following code:

var myDate = new Date();
myDate.setFullYear(2023);
myDate.setMonth(3); // Note: Months start from 0 in JavaScript, so 3 represents April
myDate.setDate(5);
myDate.setHours(9);
myDate.setMinutes(30);
myDate.setSeconds(0);

In this example, we first create a new Date object using the new Date() constructor, which creates a Date object with the current date and time. Then, we use the various Date setter methods to set the year, month, day, hour, minute, and second to the desired values.

Using these setter methods allows you to easily modify specific parts of a Date object without having to create a new Date object from scratch.

what is Internationalization Dates

Internationalization of dates in JavaScript refers to the process of displaying and formatting dates according to the conventions followed by people in different regions of the world. This ensures that the date representation is easily understandable and readable by people from different cultures and languages.

For example, in the United States, the date format is typically MM/DD/YYYY (month/day/year), whereas in many European countries, the format is DD/MM/YYYY (day/month/year). Similarly, the way days and months are abbreviated, the use of 12-hour or 24-hour clock, and the inclusion or exclusion of leading zeros may differ between regions.

To handle these variations, JavaScript provides a built-in object called "Intl" that includes various properties and methods for internationalizing dates. Here's an example of how to use the "Intl.DateTimeFormat()" method to format a date in Hinglish, which is a mixture of Hindi and English:

// Create a new date object
const date = new Date('2022-07-15');

// Create a formatter object with the desired language and options
const formatter = new Intl.DateTimeFormat('hi-IN', {
  weekday: 'long',
  year: 'numeric',
  month: 'long',
  day: 'numeric',
  hour: 'numeric',
  minute: 'numeric',
  timeZoneName: 'short'
});

// Format the date using the formatter object
const formattedDate = formatter.format(date);

// Output the formatted date
console.log(formattedDate);

In this example, we create a new date object representing July 15th, 2022. We then create a formatter object using the "Intl.DateTimeFormat()" method and pass in the language code for Hindi (hi) and India (IN), as well as several options for how we want the date to be formatted. Finally, we call the "format()" method on the formatter object to convert the date into a string using the specified format.

The resulting output might look something like this:

शुक्रवार, १५ जुलाई २०२२, २:३० PM GMT+5:30

This represents the date and time in a way that is familiar and easily understandable to someone who speaks Hinglish. The weekday is spelled out in full (शुक्रवार), the month is spelled out in full (जुलाई), and the time includes the AM/PM indicator as well as the time zone (GMT+5:30).

what is setTimeOut and setInterval

setTimeout aur setInterval dono JavaScript mein time-based functions hote hai jo code ko delay karne ya phir repeat karna kaam karte hai.

setTimeout ek function hai jo ek delay ke baad ek baar code ko chalata hai. Yeh function do arguments leta hai - ek callback function aur ek time duration in milliseconds. Callback function, jo ki chalaya jayega jab delay complete ho jayega, setTimeout function se ek unique identifier return karta hai jo hum cancel kar sakte hai agar chahiye.

Iska ek example hai:

setTimeout(function() {
  console.log("Hello, world!");
}, 3000);

Yahan pe, setTimeout function function() ko 3 seconds (3000 milliseconds) ke baad run karega. Iske baad "Hello, world!" console pe print hoga.

setInterval bhi ek function hai jo code ko delay karne ke saath-saath regularly repeat karta hai. setInterval bhi do arguments leta hai - ek callback function aur ek time duration in milliseconds. setInterval function se bhi unique identifier return hota hai jo hum cancel kar sakte hai.

Iska ek example hai:

var count = 0;
var intervalId = setInterval(function() {
  console.log(count);
  count++;
  if (count === 5) {
    clearInterval(intervalId);
  }
}, 1000);

Yahan pe, setInterval function function() ko 1 second (1000 milliseconds) ke baad run karega. Jaise hi wo chalega, yeh count variable ko 1 increase karega aur usko console pe print karega. Agar count 5 ke equal ho jata hai, to setInterval ko clearInterval se cancel kar diya jayega.