Javascript: add year to the current timestamp [duplicate] - javascript

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

Related

javascript convert date in the future to timestamp [duplicate]

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);

How to get DateTime.Now.TimeOfDay.ToString(“HH:mm:ss.ffffff”) in Javascript [duplicate]

This question already has answers here:
Javascript Date toString convert and format to local time
(3 answers)
Closed 3 years ago.
I would like to get date format like "20/06/2019 13:20:04.8517209" in Javascript
Try this:
d = new Date()
date = d.toLocaleString() + "." +d.getMilliseconds()
output: "20/06/2019, 13:22:03.517"
For cleaner version try to use moment js: https://momentjs.com/
moment(d).format('YYYY-MM-DDTHH:mm:ss.SSSSSSS')

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));

Getting date of next Monday in dd/mm/yyyy [duplicate]

This question already has answers here:
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
Closed 5 years ago.
I got this JS code:
var d = new Date();
d.setDate(d.getDate() + (1 + 7 - d.getDay()) % 7);
document.write(d);
and I want JS to display it in dd/mm/yyyy. How can I do that?
Thank you,
Till
Try this link to W3 Schools date formatting:
https://www.w3schools.com/js/js_date_formats.asp
and date methods:
https://www.w3schools.com/js/js_date_methods.asp
and check out this previous S/O answer:
How to convert a full date to a short date in javascript?

Categories

Resources