Add 30 days to the time now [duplicate] - javascript

This question already has answers here:
Add 30 days to date (mm/dd/yy)
(4 answers)
Closed 6 years ago.
I'm using Date.now() to get the current date.
I then need to add 30 days to this.
How can I do this?
Would I just work out how many seconds in 30 days, then add this on?

var d = new Date();
console.log(d);
d.setDate(d.getDate() + 30);
console.log(d);

Date.prototype.getDate returns current date day number.
Date.prototype.setDate set date number of given date to value passed to it.
If value exceeds normal date range of month date extra value will handle needed math and change month (or year if necessary) and shows correct result.
var date = new Date;
date.setDate(date.getDate() + 30);
console.log(new Date, date);

Related

get current month prints previos month [duplicate]

This question already has answers here:
getMonth in javascript gives previous month
(6 answers)
Closed 2 years ago.
I want to get the current month. we are in may now but a weird thing happens:
var thisDate = new Date();
console.log(thisDate);
console.log(thisDate.getMonth());
Output:
Thu May 14 2020 12:25:19
4
The expected value is 5
getMonth() return number from 0 to 11, so just add +1 to that.
The getMonth() method returns the month in the specified date according to local time, as a zero-based value (where zero indicates the first month of the year).
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getMonth
Use this code
var thisDate = new Date();
console.log(thisDate);
console.log(thisDate.getMonth() + 1);

How can i get first and last day of current week in ISO Date format in javascript? [duplicate]

This question already has answers here:
JavaScript - get the first day of the week from current date
(20 answers)
Closed 4 years ago.
$gte: ISODate("2010-04-29T00:00:00.000Z"),$lt: ISODate("2010-05-01T00:00:00.000Z")
You may get the day of the week by calling .getDay() on the date object. Once you have the day of the week, rest are arithmetic operations.
To convert Date to ISODateFormat you can use toISOString
var dateForCalculation = new Date();
var prevSunday = new Date(dateForCalculation.setDate(dateForCalculation.getDate()-dateForCalculation.getDay())).toISOString();
var nextSunday = new Date(dateForCalculation.setDate(dateForCalculation.getDate()+7)).toISOString();
console.log(prevSunday);
console.log(nextSunday);

how to convert particular time into UTC timestamp [duplicate]

This question already has answers here:
How do I get a timestamp in JavaScript?
(43 answers)
How to set Hours,minutes,seconds to Date which is in GMT
(3 answers)
Closed 5 years ago.
I'm having a time in this format like
var time = "22:00:00"
I need to convert this into UTC time format like 1567890764
I'm using this one to convert time into UTC.
Math.floor(new Date().getTime() / 1000) //it return current UTC timestamp
how to pass the "time" variable and get UTC timestamp for that particular time?
You can just create UTC Timestamps with the full date.
You can use the .getTime() function of the Date object. You will get the milliseconds since 1970/01/01 and then divide it with 1000 to get the seconds.
let datestring = "2017-10-21 13:22:01";
let date = new Date(datestring);
console.log(date.getTime() / 1000); // will return 1508584921
You can also take a look at Moment.js which is great for handling Date and Time in Javascript.
EDIT:
If you want todays date 22:00:00 just init new Date and set the time like this:
let date = new Date();
date.setHours(22);
date.setMinutes(0);
date.setSeconds(0);

Javascript date parse error? [duplicate]

This question already has answers here:
javascript is creating date wrong month
(4 answers)
Closed 5 years ago.
let date = new Date("2017-09-12T12:00:00");
console.log(date.getUTCMonth());
Here I am expecting it would log 09 for a month but it's logging 08.
Year, day , hour and minute gets parsed correctly though. what's going on here? How can I extract 09 from the above date string?
The getUTCMonth() is a zero-based value — zero is January.
For more info: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getUTCMonth
date.getUTCMonth() method returns the month (from 0 to 11) for the specified date.
So to get what you expect, you should add 1.
Months are 0-indexed in Javascript.
var date = new Date(date),
month = '' + (date.getMonth() + 1),
day = '' + date.getDate(),
year = date.getFullYear();
For a quick example is how you would format it if you wanted to format it in a YYYY - MM - DD format.

Incorrect Date() outcome [duplicate]

This question already has answers here:
getMonth in javascript gives previous month
(6 answers)
Closed 9 years ago.
I'm using the javascript Date() function to work with dates.
I have the following:
var date = new Date();
var d = date.getDate();
var m = date.getMonth();
var y = date.getFullYear();
console.log(y);
console.log(m);
console.log(d);
This returns, in the console:
2013
9
1
Which correct me If I'm wrong, is 1 month behind the actual date of today.
I'm in the UK, so could BST be affecting the outcome of this?
Thanks
var m = date.getMonth();
Because month begins at 0.
0-January
1-February
2-March
3-April
4-May
5-June
6-July
7-August
8-September
9-October
10-November
11-December
JavaScript months are 0 based.
Date.getMonth
Returns the month in the specified date according to local time, as a
zero-based value (where zero indicates the first month of the year).
Just to be confusing!
So always remember to add 1 to the result of date.getMonth() if you want the actual month number.
The getMonth() method returns the month (from 0 to 11) for the specified date, according to local time.
Source: w3schools
So, you are getting 1 month less than the actual month of your local time.
You can use:
console.log(("0" + (m+1)).slice(-2))
JavaScript month start from 0.

Categories

Resources