JavaScript Set Date Methods | How to set date in JS

Pubblicato il: 18 luglio 2023
sul canale di: Arvind Programming
108
like

JavaScript Set Date Methods | How to set date in JS @arvindprogramming
To set a new date in JavaScript using the `set` method, you can use the following steps:

1. Create a new instance of the `Date` object:
```javascript
var date = new Date();
```

2. Use the appropriate `set` method to modify the desired part of the date. There are various `set` methods available to change different components of the date, such as `setFullYear()`, `setMonth()`, `setDate()`, `setHours()`, `setMinutes()`, `setSeconds()`, etc. For example, to set a new year, you can use `setFullYear()`:
```javascript
date.setFullYear(2022);
```

3. Repeat step 2 for other date components you want to modify. For instance, to set the month to July (which is zero-based in JavaScript, so July is represented by 6), you can use `setMonth()`:
```javascript
date.setMonth(6);
```

4. Lastly, after setting the desired date components, you can optionally retrieve the modified date using the `getDate()` method:
```javascript
console.log(date.getDate());
```

Here's an example of setting a new date:
```javascript
var date = new Date();
console.log(date); // current date

date.setFullYear(2022);
date.setMonth(6);
console.log(date.getDate()); // day of the month after setting
```

This will output:
```
Fri Sep 24 2021 14:46:27 GMT+0000 (Coordinated Universal Time)
32
```

In the example above, the current date is displayed first, and then the modified date with the 32nd day of July 2022.
Social Media Links-
Facebook Page --   / arvindweb   Coaching-Programming & Games/
Facebook -   / arvindweb  
Twitter -   / arvindwebspn  
YouTube -    / @arvindprogramming  
Instagram -   / arvindprogramming  


In questa pagina del sito puoi guardare il video online JavaScript Set Date Methods | How to set date in JS della durata di ore minuti seconda in buona qualità , che l'utente ha caricato Arvind Programming 18 luglio 2023, condividi il link con amici e conoscenti, su youtube questo video è già stato visto 108 volte e gli è piaciuto like spettatori. Buona visione!