javascript convert date in the future to timestamp [duplicate] - javascript

This question already has answers here:
Add A Year To Today's Date
(9 answers)
Closed 3 years ago.
I want to get the timestamp of one-year from now:
var oneYr = new Date();
oneYr.setYear((new Date()).getYear() + 1);
When I try to get the timestamp:
oneYr.getTime() /1000
I get -58351759111000 (not correct)
It's only working for date in the past.
Any idea how to get timestamp for future date?

Use getFullYear instead of getYear.
var oneYr = new Date();
oneYr.setYear((new Date()).getFullYear() + 1);

Related

Javascript: add year to the current timestamp [duplicate]

This question already has answers here:
Add A Year To Today's Date
(9 answers)
Closed 2 years ago.
i'm looking to handle a script wich has to add 10 years to the current timestamp in javascript...
But when i try this:
Date.now() + /* 10 years in seconds */ 315569520
It doens't work...
Is there anyway to implement it ?
Do like:
const dt = new Date;
dt.setFullYear(dt.getFullYear()+10);
console.log(dt.toString());
plz check this url. You can find your solution.
https://www.w3resource.com/javascript-exercises/javascript-date-exercise-41.php
it's very simple you can achieve in two line code:
`let timestamp = new Date();
timestamp.setFullYear(timestamp.getFullYear() + 10);
console.log(timestamp);`
You need to use a Date constructor to create the date from the value you calculated.
new Date(Date.now() + 315569520 * 1000)
You need to add the value of 10 years in milliseconds, and then pass the new value to a Date constructor

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 would I get tomorrow's day of the month using JS? [duplicate]

This question already has answers here:
Incrementing a date in JavaScript
(19 answers)
Closed 4 years ago.
I want to get the next day's date in Javascript. I can't find anything that will return it like the getDate() function. Thanks!
There is something like that, called setDate().
var date = new Date();
date.setDate(date.getDate() + 1);
console.log(date);

Invalid date when I put a future timestamp [duplicate]

This question already has answers here:
How to convert milliseconds to date and time?
(3 answers)
Closed 5 years ago.
Suppose to have a future timestamp date.
var timestamp="1511858535000" //Future timestamp
var date=new Date(timestamp);
console.log(JSON.stringify(date));// this line give me Invalid Data
Anyone can help me to understand why?
The timestamp you mentioned in the comments works fine, but you have to pass it into the Date constructor as a number, not a string:
var timestamp = "1511858535000";
var date = new Date(Number(timestamp));
console.log(JSON.stringify(date));

Get the previous date from today's date in javascript [duplicate]

This question already has answers here:
Javascript code for showing yesterday's date and todays date
(6 answers)
Closed 6 years ago.
I want to plot a graph by fetching the previous seven days using the present day in Javascript.
Which should pass through the tests of Leap year, days of mont(30/31), Year change.
Thank you in advance.
var d = new Date();
var yesterday = d.setDate(d.getDate() - 1);
Simply replace - 1 with - 2, - 3 and so on for the previous seven days.

Categories

Resources