I've got something like this DateTime.Now.ToString("dd.MM.yy");
In my code, And I need to add 1 week to it, like 5.4.2012
to become 12.4.2012
I tried to convert it to int and then add it up, but there is a problem when it's up to 30.
Can you tell me some clever way how to do it?
Use date. getDate() to retrieve the defined days for the given date . Then add your desired weeks and adjust the date using the setDate() .
getDate() method. You can then go ahead and add 7 more days to increase the days by one week. Increase the days of a JavaScript date instance using the . setDate() method.
function addWeeks(numOfWeeks, date = new Date()) { date. setDate(date. getDate() + numOfWeeks * 7); return date; } // Add 2 weeks to current Date console. log(addWeeks(2)); // 👇️ Mon Feb 28 2022 console.
The setDate() method can be used to add days to a date.
You want to leave it as a DateTime
until you are ready to convert it to a string.
DateTime.Now.AddDays(7).ToString("dd.MM.yy");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With