Get timestamp of current hour in Javascript [duplicate] - javascript

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

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!

What is difference between this two time formats and how to convert [duplicate]

This question already has answers here:
How to output date in javascript in ISO 8601 without milliseconds and with Z
(6 answers)
Closed 3 years ago.
I have a date like this from database: "2019-12-06T00:00:00.000Z".
But I need it like "2019-12-06T00:00:00Z".
I tried to convert first one to date and to ISO string but I don't get what I need.
let s = "2019-12-06T00:00:00.000Z";
let dt = new Date(s).toISOString();
This print same.
The number 00:00:00.000 are hours, minutes, seconds and miliseconds respectively. So to remove miliseconds
Try this:
let s = "2019-12-06T00:00:00.000Z";
let dt = new Date(s).toISOString().split('.')[0]+"Z" ;
console.log(dt);

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.

Javascript: Finding the difference between a datetime object and current time in seconds [duplicate]

This question already has answers here:
Get time difference between two dates in seconds
(10 answers)
Closed 7 years ago.
Lets say I have a date-time object 2015-12-31T12:59. Is there a way in JavaScript to find the difference between current date-time and the above date-time object in seconds? Basically, is there a way to find out the time in seconds from this very moment till the date-time specified by a future date-time object?
I did some digging but couldn't find anything that could be of any use for me in this case.
To achieve this you can simply subtract the date object from Date.now(). Then take the millisecond value that it gives you, and divide by 1000 to get the second value. Here is a live example:
var date1 = new Date("2015-12-31T12:59");
var date2 = Date.now();
document.getElementById("output").innerHTML = (date1 - date2) / 1000;
Seconds between now and (2015-12-31T12:59): <span id="output"></span>

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

Categories