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

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

Related

Subtract hours from date and time [duplicate]

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

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!

Javascript Determine 10 days before specific date [duplicate]

This question already has answers here:
How to add days to Date?
(56 answers)
Closed 4 years ago.
So I am currently fetching some records from my database, and one of the records is a 'last edited' which is a date. However, I just want to display this 10 days before it has been exactly one year since the specific date. How would I approach this? I know how to deal with arrays and the fetching itself, but I'm not sure how I would deal with the date?
This is what I've done so far:
var startDate = fetchedDate;
var validDate = new Date(startDate);
var fullYear = validDate.getFullYear();
validDate.setFullYear(fullYear + 1);
And then I need to create a new date to compare it, I suppose? But how? I also want to know how many days it is until it has been one year.
If you wanna make it with pure javascript you can
var startDate = fetchedDate;
var validDate = new Date(startDate);
validDate.setYear(validDate.getFullYear() - 1);
validDate.setDate(validDate.getDate() - 10);
But I also will recommend you to use moment.js, is a very good javascript library to handle dates, otherwise sometimes is like trying to reinvent the wheel.

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

Get time in javascript like python time.time() [duplicate]

This question already has answers here:
How do I get a timestamp in JavaScript?
(43 answers)
Closed 9 years ago.
In python I can use time.time() to get this:
time = time.time()
time = 1362914221
How do I get this in java-script?
time = ????
You can use
time = (new Date()).getTime() / 1000;
See getTime() on the MDN.
Just like this:
var time = +new Date;

Categories