Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript - get next day of a string

I've a var example = "05-10-1983"

How I can get the "next day" of the string example?

I've try to use Date object...but nothing...

like image 538
Tommaso Taruffi Avatar asked Jul 21 '09 18:07

Tommaso Taruffi


People also ask

How to get the day of a specified date in JavaScript?

The JavaScript Date object provides various methods to access the day, month and year along time. For example, to get the day of a specified date you will have use the method getDay().

How to get tomorrow’s date in a string format in JavaScript?

- GeeksforGeeks How to get tomorrow’s date in a string format in JavaScript ? In this article, we will see how to print tomorrow’s date in string representation using JavaScript. To achieve this, we use the Date object and create an instance of it. After that by using the setDate () method, we increase one date to the present date.

How to return the day of the week in JavaScript?

Onclick of the button fires the function myDate () in the script code at the same time getDay () return the name of the present day as output. Returning day (between 0 to 6) of the week. We are returning the day (Number) of a week by using getDay () method in JavaScript.

What is getday () method in JavaScript?

JavaScript getDay () Method 1 Definition and Usage. The getDay () method returns the day of the week (from 0 to 6) for the specified date. ... 2 Browser Support 3 Syntax 4 Parameters 5 Technical Details 6 More Examples 7 Related Pages


1 Answers

This would do it for simple scenarios like the one you have:

var example = '05-10-1983';
var date = new Date();
var parts = example.split('-');
date.setFullYear(parts[2], parts[0]-1, parts[1]); // year, month (0-based), day
date.setTime(date.getTime() + 86400000);
alert(date);

Essentially, we create an empty Date object and set the year, month, and date with the setFullYear() function. We then grab the timestamp from that date using getTime() and add 1 day (86400000 milliseconds) to it and set it back to the date using the setTime() function.

If you need something more complicated than this, like support for different formats and stuff like that, you should take a look at the datejs library which does quite a bit of work for you.

like image 178
Paolo Bergantino Avatar answered Sep 18 '22 22:09

Paolo Bergantino