Subtract hours from date and time [duplicate] - javascript

This question already has answers here:
How to add hours to a Date object?
(20 answers)
How do I subtract minutes from a date in JavaScript?
(9 answers)
want to substract 5.30 hours from a Date javascript
(1 answer)
Closed 2 years ago.
Could someone help me with this. I need to finish something for a schoolproject which we are displaying tonight and I have to subtract 2 hours from this code. Javascript is totally not my thing.
<span id="datetime"></span>
<script>
var dt = new Date();
document.getElementById("datetime").innerHTML = dt.toLocaleString();
</script>
I have no idea how to change this. Thanks in advance.
Regards,
Justin

Try
var dt = new Date();
dt.setHours(dt.getHours() - 2);
document.getElementById("datetime").innerHTML = dt.toLocaleString();
Have a look here https://codepen.io/vyspiansky/pen/abNBqrx

You can use moment.js.
moment(dt).subtract(2, 'hours').format();

Related

Getting the date of the nth day after a given date [duplicate]

This question already has answers here:
Incrementing a date in JavaScript
(19 answers)
How to add days to Date?
(56 answers)
Closed 2 years ago.
I am looking to find the date for, say, the 100th day after any given day. Is there a way to do that via javascript?
I was hoping it could be as simple as below, but it isn't quite that simple.
var givenDay = new Date(01/01/2020);
var hundredthDay = new Date(givenDay + 100);
console.log(hundredthDay)
You can try using .setDate() and .getDate() combination.
The setDate() method sets the day of the Date object relative to the beginning of the currently set month.
The getDate() method returns the day of the month for the specified date according to local time.
Adding the required days to .getDate() as the following:
const givenDay = new Date('01/01/2020');
console.log(givenDay);
const result = new Date(givenDay.setDate(givenDay.getDate() + 1 + 100));
console.log(result);
I hope this helps!

Get timestamp of current hour in Javascript [duplicate]

This question already has answers here:
How do I get a timestamp in JavaScript?
(43 answers)
Closed 7 years ago.
Is it possible for me to get the UTC time of current hour in Javascript?
I've tried the following:
var mins = new Date().getMinutes();
var hours = new Date().getHours();
var hourStamp = new Date().setHours(hours, 0,0,0);
var dates = new Date(hourStamp);
Here dates gives me the Unix Timestamp of the current hour. Is there a better/faster way to do this?
EDIT:
For example: I want the Timestamp for the current hour. So if it's 00:16 am on 12/04/2015, I want the timestamp of 12/04/2015 00:00. And, my method works. My question pertains to if there's a better method of doing the same.
var UTCHour = new Date().getUTCHours();
Like this?
The one works well:
var UTCTime= (new Date()).toUTCString();

Javascript convert Datetime to Time [duplicate]

This question already has answers here:
extract time from datetime using javascript
(10 answers)
Closed 8 years ago.
Is it possible to get the time from the following datetime in javascript?
2014-11-24 08:30:00
Any help would be appreciated.
UPDATE
Found the solution to my problem in the duplicate:
datetime.substr(11, 5);
//returns 08:30
Thanks for all your help!
What about slice? It should be the fastest when you know you always get datetime (and time is in last 8 chars).
var datetime = "2014-11-24 08:30:00";
var time = datetime.slice(-8);
alert(time); // returns "08:30:00"
new Date("2014-11-24 08:30:00").getTime();
This code block will get time from your exact date.

javascript - Get the time out of my browser [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to get current date in JavaScript
How can i Get the time out of my browser whit using javascript?
This do I need to make a comparison in my script.
currentTime = new Date();
time = currentTime.getTime();
hours = currentTime.getHours();
etc
You can use the Date object to get the current time:
var currentTime = new Date();

Add day(s) to a Date object [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to add number of days to today's date?
I'm confused, I found so many different approaches, and which of them is actually correct?
So what is the correct way to add day(s) to a given Date?
date.setTime( date.getTime() + days * 86400000 );
Note : Use it if calculating / adding days from current date.
Be aware: this answer has issues (see comments)
var myDate = new Date();
myDate.setDate(myDate.getDate() + AddDaysHere);
It should be like
var newDate = new Date(date.setTime( date.getTime() + days * 86400000 ));

Categories