Consider the following code of HTML & JavaScript
<!DOCTYPE html>
<html>
<body>
<script>
var str = "20990229";
var showDate = new Date();
showDate.setFullYear(str.substring(0, 4))
showDate.setMonth(parseInt(str.substring(4, 6), 10) - 1)
showDate.setDate(str.substring(6, 8))
document.write(showDate)
</script>
</body>
</html>
Output:
Fri Mar 01 2099 16:02:52 GMT+0530 (India Standard Time)
The output is not the correct one, where I am going wrong is not known.
Could anyone tell me where I am going wrong?
The output is exactly as it should be:
var str = "20990229";
var showDate = new Date();
showDate.setFullYear(str.substring(0, 4)); // Set year to 2099
showDate.setMonth(parseInt(str.substring(4, 6), 10) - 1); // Set month to 1
showDate.setDate(str.substring(6, 8)); // Set date to 29
That would be 29th February 2099. (Note that months are indexed from 0).
Since 2099 is not a leap year there is no February 29th, and the date corresponds to March 1st.
If you change the year to one that is a leap year (such as 2096) then the output will be as you expect. Here's an example.
Related
I want to get the date from the GMT time but it returns the date which is one day ahead. How can I get the date mentioned in the GMT string always?
new Date("Mon, 27 Aug 2018 22:00:00 GMT").getDate()
This command returns 28 as the output, but I want 27.
Is there anything I need to add?
Thanks in advance.
Try this One.I think your problem will be solved.
<script>
function myFunction() {
var d = new Date();
var n = d.getUTCDate();
document.getElementById("demo").innerHTML = n;
}
</script>
When you create a new Date() the browser returns date based on your device timezone. You can use Date.getTimezoneOffset() to get GMT offset time difference and then adjust the time by multiplying the value.
// Your date
var myDate = new Date("Mon, 27 Aug 2018 22:00:00 GMT")
// Convert your date to local using getTimezoneOffset() and multiply with 60000 to get time adjusted GMT 0
var myDateLocal =new Date( myDate.valueOf() + myDate.getTimezoneOffset() * 60000 ).getDate();
document.getElementById("myDate").innerHTML=myDateLocal;
<h1 id="myDate" ></h1>
I am getting invalid date when I set month as 5 in JavaScript date object.
I know that Month is zero based. so, month 5 means June.
But I am getting month as July.
<!DOCTYPE html>
<html>
<body>
<p>Click the button to display the date after changing the month.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo1"></p>
<p id="demo"></p>
<script>
function myFunction() {
var d = new Date();
d.setYear(2017);
d.setMonth(5);
d.setDate(30);
document.getElementById("demo1").innerHTML = d.getMonth();
document.getElementById("demo").innerHTML = d;
}
</script>
</body>
</html>
Output:
6
Sun Jul 30 2017 15:11:20 GMT+0200 (W. Europe Daylight Time)
But, Ideally the output should be:
5
Sat Jun 30 2017 15:11:20 GMT+0200 (W. Europe Daylight Time)
Here is what happens:
var d = new Date();
//d is today : which is 31st of May (day of your test)
d.setMonth(5)
//5 is June (setMonth starts at 0)
//As 31st of June doesn't exist, it is set as 1st of July.
d.setDate(30)
//d is now 30th of July
You'd better use the full Date constructor:
new Date(year, month, date);
Today is May 31st 2017.
When you set the month to 5 which is June, the day is still 31 but there is no June 31. At this point behavior is undefined or at least unclear; anything could happen really.
To avoid it, create the date like this:
var date = new Date(2017, 5, 30);
This is because setMonth will also set the day, but we are on 31 and June only contains 30 days, so the date will change to 1 July. Then on July 30th thanks to the setDay. Try to set the day before, normally it should work.
I have this code:
var nextDate = new Date("2016 01 31");
nextDate.setMonth(nextDate.getMonth() + 1);
I'm expecting the result to be Feb 28 2016, but it shows Mar 02 2016 instead.
Why? Is there any solution for it?
There is only 29 day in February, therefore, February 31 February will translate to Mars 2.
You need to update the days in your date object to the last day of that month. You can get the last day of the month by specifying a function that sets the date to 0:
function daysInMonth(month,year) {
return new Date(year, month, 0).getDate();
}
This is because February has 29 days, and when you set new month from January, which has 31 day, to February then the difference of the days are transferred to another month.
Easy way to do it is just create new Date instance.
You might need to implement some logic to get corresponding dates right
Possible work around with a helper function: after setMonth, check if the results doesn't contain a month equal to the expected month and if so, use setDate(0), which sets the date to last day of the previous month. e.g.
Date.prototype.addMonths = function(months){
var m = this.getMonth() + (months || 1);
this.setMonth(m);
if(this.getMonth() !== (m % 11)) //11: month is 0 based
this.setDate(0);
}
var nextDate = new Date("2016 01 31");
nextDate.addMonths(1);
document.writeln(nextDate);
months || 1 only is meant to have a default value if no month was submitted. m % 11 is needed in case of year transitions. 11 and not 12 because javascripts month (and thus getMonth) is 0 based.
I need to convert a hard coded date into a standard GMT format.How can I do this?
The date I have is in the following format:
var myDate = 'dd|mm|yyyy';
There is no time or day description in the date.Just the 'dd|mm|yyyy' string.
Is there a way I can convert it into GMT?
Thanks in advance.
a = '22/02/2014'.split('/')
d = new Date(a[2],parseInt(a[1], 10) - 1,a[0])
//Sat Feb 22 2014 00:00:00 GMT+0530 (India Standard Time)
Now you have a javascript date object in d
utc = d.getUTCDate() + "/" + (d.getUTCMonth() + 1 ) + "/" + d.getUTCFullYear();
//"21/2/2014" for an accurate conversion to UTC time of day is a must.
If you are in say India, the Javascript Date object will have timeZoneOffset 330. So its not possible to keep a javascript Date object with timezone GMT unless your system time is GMT.
So if you want a Date object for calculation, you can create one with localTimezone and simply suppose it is GMT
pseudoGMT = new Date( Date.parse(d) + d.getTimezoneOffset() * 60 * 1000);
//Fri Feb 21 2014 18:30:00 GMT+0530 (India Standard Time)
If you can explain your high level requirement we might be able to help with some alternate solutions.
Use regex matching to extract the data you need:
var myDate = "21|01|2014";
var data = myDate.match(/(\d{2})\|(\d{2})\|(\d{4})/);
var date = new Date(data[3], data[2] - 1, data[1]);
Note that the month is 0-indexed, so january = 0
More on regular expressions: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp
I have the following javascript code:
<script type="text/javascript">
$(function () {
var currentDateTime = new Date();
var oneYear = new Date();
oneYear.setYear(oneYear.getYear() + 1);
alert(currentDateTime + "_" + oneYear);
});
</script>
i would expect the alert to output the current datetime and the datetime of one year from now. However I get this in the alert: "Fri Oct 22 2010 14:17:31 GMT-0400 (Eastern Daylight Time)_Thu Oct 22 0111 14:17:31 GMT-0400 (Eastern Daylight Time)"
Clearly it's not adding "1" to the Year correctly!
Whats going on? How did it become the year 0111???
It is correct. .getYear() returns "actual year − 1900". 2010 − 1900 = 110.
Use .getFullYear() instead. .getYear() has been deprecated for a long time.
Y2K was 10 years ago, but you're still using getYear instead of getFullYear? tsk tsk...
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/getFullYear
Instead of .getYear() try .getFullYear()