javascript date adding days getting wrong days [duplicate] - javascript

This question already has answers here:
How to add days to Date?
(56 answers)
Closed 5 years ago.
I am using this code on the page to show when the next few dates are, they are always the next few days... but it is not working now:
<script>var now = new Date();
var day = ("0" + (now.getDate()+3)).slice(-2);
var day2 = ("0" + (now.getDate()+4)).slice(-2);
var month = ("0" + (now.getMonth() + 1)).slice(-2);
var today = (month)+"/"+(day)+"/"+now.getFullYear();
var today2 = (month)+"/"+(day2)+"/"+now.getFullYear();
document.write(today); document.write(' and/or ');document.write(today2);
</script>
But it is putting this out:
currently scheduled for: 05/32/2017 and/or 05/33/2017
how do I get the + 1 to have it go to the next month if it needs to?

Try this
<script>
var now = new Date();
var day = ("0" + (now.setDate(now.getDate()+3)).getDate()).slice(-2);
var day2 = ("0" + (now.setDate(now.getDate()+4)).getDate()).slice(-2);
var month = ("0" + (now.setMonth(now.getMonth() + 1+1)).getMonth()).slice(-2);
var today = (month)+"/"+(day)+"/"+now.getFullYear();
var today2 = (month)+"/"+(day2)+"/"+now.getFullYear();
document.write(today); document.write(' and/or ');document.write(today2);
</script>

Related

Creating and calling dynamic date link in html [duplicate]

This question already has answers here:
Why does jQuery or a DOM method such as getElementById not find the element?
(6 answers)
Closed 1 year ago.
I have some code which generates a dynamic date string, I want to put in into a web link so I can call the link to generate a new image each day.
I have written this code:
<script>
var d = new Date();
var year = d.getFullYear();
var month = d.getMonth() + 1;
var day = d.getDate();
document.getElementById("link").href = "http://earth.nullschool.net/#" + year + "/" + month + "/" + day + "/1200Z/wind/isobaric/500hPa/";
</script>
500 mb Earth Wind Map
However when I click this link it goes to the old date (2012), not the current date that the javascript creates. How do I create the link with today's date inside it?
You need to place the anchor tag before the script.
500 mb Earth Wind Map
<script>
var d = new Date();
var year = d.getFullYear();
var month = d.getMonth() + 1;
var day = d.getDate();
document.getElementById("link").href = "http://earth.nullschool.net/#" + year + "/" + month + "/" + day + "/1200Z/wind/isobaric/500hPa/";
</script>
I am not sure if this works but it should so for your last line you can do this instead
<a href="http://earth.nullschool.net/#{}/{}/{}/1200Z/wind/isobaric/500hPa/".format(year,month,day) id="link">500 mb Earth
Since you are using a format method u are substituting your variable into the brackets.

Angularjs getcurrent time [duplicate]

This question already has answers here:
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
Closed 3 years ago.
I am using the below code to get the current time in angularJS:
$scope.getDatetime = function() {
return (new Date()) + "abc.txt" ;
};
What is the correct code to the current time in YYYY-MM-DD-Hours-Minutes-Seconds?
Maybe you need this.
$scope.getDatetime = function() {
var date = new Date();
var day = date.getDate();
var month = ((date.getMonth() + 1) > 9) ? date.getMonth() + 1 : "0" + (date.getMonth() + 1)
var year = date.getFullYear();
var hours = date.getHours();
var minutes = date.getMinutes();
var seconds = date.getSeconds();
return year+"/"+month+"/"+day+" "+hours+":"+minutes+":"+seconds;
};
var s = new Date().toISOString();
console.log(s);
s = s.slice(0,19);
s = s.replace('T','-');
s = s.replace(':','-');
s = s.replace(':','-');
console.log(s);

Adding Day to Date [duplicate]

This question already has answers here:
How to add number of days to today's date? [duplicate]
(16 answers)
Closed 5 years ago.
I have an example below of code that getting the date and adding certain days.
But the result I got from log is like these 1507824000000.
var endDate = new Date('10/03/2017');
var numOfDays = 10;
console.log(endDate.setDate(endDate.getDate() + numOfDays ));
If you want something to see your 10 days added, you can try the following :
var endDate = new Date('10/03/2017');
var numOfDays = 10;
endDate.setDate(endDate.getDate() + numOfDays);
var dd = endDate.getDate();
var mm = endDate.getMonth() + 1;
var y = endDate.getFullYear();
var yourNewDate = dd + '/'+ mm + '/'+ y;
console.log(yourNewDate)

Javascript n months after today

I am trying to get the day 90 days after today. This is my code:
var today = new Date();
var threeMonthsFromToday = new Date(today.setDate(today.getDate() + 90));
When I print threeMonthsFromToday, I get the correct date: 2017-04-24T15:17:42.641Z. However, when I try to reformat the date to be in the form dd/mm/yyyy using this code:
var day = ('0' + threeMonthsFromToday.getDate()).slice(-2);
var month = ('0' + threeMonthsFromToday.getMonth() + 1).slice(-2);
var year = threeMonthsFromToday.getFullYear();
var date = day + '/' + month + '/' + year;
I get a completely different and invalid date: 24/31/2017.
I have been debugging this for hours and still can't seem to figure what I am doing wrong.
Well, '0' + threeMonthsFromToday.getMonth() give you a string : "03" then you add 1 converted to string giving you "031" for month before slice.
Use this :
var month = ('0' + (threeMonthsFromToday.getMonth() + 1)).slice(-2);
You are missing the basic BODMAS rule here please modify your code as follows
var today = new Date();
var threeMonthsFromToday = new Date(today.setDate(today.getDate() + 90));
var day = ('0' + threeMonthsFromToday.getDate()).slice(-2);
var month = ('0' + (threeMonthsFromToday.getMonth() + 1)).slice(-2);
var year = threeMonthsFromToday.getFullYear();
var date = day + '/' + month + '/' + year;
the operations are performed from left to right, so month is getting converted to string before being added to a number. Including a bracket will first perform operation inside bracket and then make it a string
Can you use toLocaleString?
threeMonthsFromToday.toLocaleDateString('en-GB')
Below does the trick for you ...
getMonth() +1 before adding the "0" to it so that you get an arithematic +1
var today = new Date();
var threeMonthsFromToday = new Date(today.setDate(today.getDate() + 90));
var day = ('0' + threeMonthsFromToday.getDate()).slice(-2);
var month = ('0' + (threeMonthsFromToday.getMonth()+1)).slice(-2);
var year = threeMonthsFromToday.getFullYear();
var date = day + '/' + month + '/' + year;
console.log(date);
This should work.
var day = threeMonthsFromToday.getDate()
if(day < 10){
day = '0' + day
}
var month = threeMonthsFromToday.getMonth()+1
if(month<10){
month = '0' + month
}
var year = threeMonthsFromToday.getFullYear()
var date = day + '/' + month + '/' + year
Use Simple toLocaleDateString method
The toLocaleDateString() method returns a string with a language sensitive representation of the date portion of this date.
var today = new Date();
var threeMonthsFromToday = new Date(today.setDate(today.getDate() + 90));
var date = threeMonthsFromToday.toLocaleDateString();
console.log(date);
//result in console : "24/04/2017"
Try it out on your console.

Inaccurate date function in javascript under node.js [duplicate]

This question already has answers here:
unexpected javascript date behavior
(3 answers)
Closed 7 years ago.
I have this JS:
var d = new Date();
var year = d.getFullYear();
var month = d.getMonth();
var day = d.getDay();
var hours = d.getHours();
var minutes = d.getMinutes();
var seconds = d.getSeconds();
var milliseconds = d.getMilliseconds();
var timeStamp = year + '.' + month + '.' + day + '. ' + hours + ':' + minutes + ':' + seconds + ':' + milliseconds;
What it's outputting:
2015.10.5. 11:45:22:307
But what it should be outputting:
2015.11.5. 11:45:22:307
What is the problem with my script? Why the month is not correct?
Thank You for your help!
because getMonth method return the month from 0 to 11
http://www.w3schools.com/jsref/jsref_getmonth.asp

Categories