This question already has answers here:
What does the plus sign do in '+new Date'
(7 answers)
Closed 8 years ago.
What is the difference between new Date and +new Date?
For example:
var date = new Date;
console.log(date);
var plusDate = +new Date;
console.log(plusDate);
Logs:
Sat May 10 2014 01:13:46 GMT+0300 (Jordan Standard Time)
1399673626539
The unary plus operator casts the Date object to a Number object (which is expressed in in milliseconds since 01 January, 1970 UTC).
The first one creates a Date object, the second one adds the current date value in milliseconds to the original value of plusDate, which was 0.
Related
This question already has answers here:
getMonth in javascript gives previous month
(6 answers)
Closed 2 years ago.
I am trying to get the month of the given date as a string in javaScript but I get the wrong month of the date displayed.
var d = new Date("Sun Dec 13 2020 08:00:00 GMT+0530");
console.log(d.getMonth());
I get in the console output for month as 11 for some reason.
Solution
The range begins at zero and not at one
So refering the mdn docs
monthIndex Integer value representing the month, beginning with 0 for
January to 11 for December.
The range from getMonth() is 0-11
So the solution is just to add +1 to the result of the getMonth() method
console.log(d.getMonth() +1 );
the count starts from 0 -january, 1-feb,...11-december
just call this and it is done.
function month(){
const today = new Date();
const options = {
month: "long"
};
return today.toLocaleDateString("en-US", options);
}
This question already has answers here:
Javascript: parse a string to Date as LOCAL time zone
(3 answers)
Closed 3 years ago.
why its showing past day date
var date = new Date('2020-01-01');
console.log(date)
Tue Dec 31 2019 19:00:00 GMT-0500 (Eastern Standard Time)
Because of your timezone settings (Easter Standard time is GMT -0500 therefore 5 hours before 2020-01-01).
Javascript Date object are timestamps - they merely contain a number
of milliseconds since the epoch. There is no timezone info in a Date
object. Which calendar date (day, minutes, seconds) this timestamp
represents is a matter of the interpretation (one of to...String
methods).
Basically it is the toString method that converts the date to your local timezone.
Source
This question already has answers here:
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
How do I format a date in JavaScript?
(68 answers)
Closed 4 years ago.
From the server I get '2019-01-19T19:11:00.000Z' I need to convert to local timezone, so that I end up with '2019-01-19T11:11:00'. My UTC offset is 8 hrs.
new Date('2019-01-19T19:11:00.000Z') produces Sat Jan 19 2019 11:11:00 GMT-0800 (Pacific Standard Time), how do I get it back to '2019-01-19T11:11:00'? Thanks
You want the date string in iso format, respecting the local time zone:
const tzoffset = (new Date()).getTimezoneOffset() * 60000;
const d = new Date('2019-01-19T19:11:00.000Z')
console.log(new Date(d - tzoffset).toISOString().split('.')[0])
console.log('2019-01-19T11:11:00')
var now = new Date();
console.log(now.toISOString().split('.')[0]);
This question already has answers here:
Why isn't "2016-02-16" equal to "2016-02-16 00:00"?
(5 answers)
Closed 6 years ago.
I am creating a date with new Date(). When I do this, it is subtracting a day. Here is the code:
var dateString = "2016-04-10";
var date = new Date(dateString);
// date = Sat Apr 09 2016 18:00:00 GMT-0600 (MDT)
What do I misunderstand? Why is the date not Apr 10 2016? How do I make this work properly?
Your timezone is GMT-6 (as revealed by the GMT-0600 (MDT) in the output you've provided). Therefore the date which gets generated is offset by -6 hours. In this case, midnight minus 6 hours is 6PM on the previous day.
If you call date.toISOString(), you'll see that the UTC time is "2016-04-10T00:00:00.000Z" as expected.
This question already has answers here:
unexpected javascript date behavior
(3 answers)
Closed 6 years ago.
I am adding days in today date and getting timestamps in milliseconds.
for(i=1;i<=shippingDays;i++){
var result = new Date();
result.setTime( result.getTime() + i * 86400000 );
console.log(result);
console.log(result.getMonth()+'-'+result.getDate()+'-'+result.getFullYear());
newDate = new Date(result.getMonth()+'-'+result.getDate()+'-'+result.getFullYear());
console.log(newDate);
};
The first console returns Thu Apr 07 2016 18:34:33 GMT+0500 (PKT) but later on result.getMonth() always returns previous month value. So the second console always returns 3-7-2016 and third console always returns Mon Mar 07 2016 00:00:00 GMT+0500 (PKT).
My ultimate goal is to get milliseconds of next days from 00:00:00. Like today is 04-06-2016. I want to get milliseconds timestamp of next few days. And time stamp should be calculated from start of that date, i.e, 00:00:00
Can any body let me know what am I doing wrong?
Months from Data.getMonth are zero based (so January is 0, feb is 1, etc).
So if you want to use the month value to make a new date just add one.
getMonth is zero based so 0=January, 1=February, 3=April etc. So the output of console.log is correct
You need to add 1 to the getMonth() function.
Example:
console.log((result.getMonth() + 1)+'-'+result.getDate()+'-'+result.getFullYear());
Will return the correct month / date. It's because the getMonth function returns a value of 0 - 11 (0 for January and 11 for December)
http://www.w3schools.com/jsref/jsref_getmonth.asp