This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Closed 2 years ago.
How can I get current date and hour like this 2020-07-06T12:30:00.
in JavaScript / react
can you help me?
So you can use builtin Date object in JS.
const today = new Date()
let day = today.getDay()
let month = today.getMonth()
let year = today.getFullYear()
console.log(`${year}-${day}-${month}`)
You can use new Date() to access the built in JS date, and to access it the way you are specifying you want the new Date().toJSON() which will give you "2020-07-06T20:25:23.671Z" for example.
You can use builtin Date object.
new Date();
You can use toJSON like
new Date().toJSON()
Related
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!
This question already has answers here:
How to add hours to a Date object?
(20 answers)
Closed 4 years ago.
I want to add hours in DateTime. I have googled it but couldn't find any code.
Here is this datetime 2018-07-25 20:23:22. I want to add hours in this datetime so it gives me new datetime in the same format
I have tried this.
var datetime = "2018-07-25 20:23:22";
datetime.setHours(datetime.getHours()+5);
But It didn't work.
Your datetime is String. You need to convert it into Date object first.
var datetime = new Date("2018-07-25 20:23:22");
console.log("Before: ", datetime);
datetime.setHours(datetime.getHours()+1);
console.log("After: ", datetime);
I would prefer to use moment.js library for manipulating and playing with dates.
Hope this may help you.
Here is the simple way with using Moment.js
const date = moment('2018-07-25 20:23:22');
date.add(5, 'h');
console.log(date.toString());
This question already has answers here:
Parsing a string to a date in JavaScript
(35 answers)
Why does Date.parse give incorrect results?
(11 answers)
Closed 5 years ago.
I have a date string and I want to convert it into a date object using date(). It's doing so but the problem is, date() processes any date as 'MM-dd-yyyy'.
I tried following code:
var myDate = '09-06-2017'; //which is a string and is in 'dd-MM-yyyy' format from my perspective
var selectedDate = new Date(myDate);
$scope.minDate = $filter('date')(selectedDate, 'yyyy/MM/dd');
console.log(selectedDate);
console.log($scope.minDate);
It will process this date as 06-09-2017 and if I pass '13-06-2017'(i.e. any value greater than 12) then it will throw 'Invalid Date' in console.
So I'm looking for a solution by which I can tell date() to process the passed string in a specific format.
Thank You.
With out using any libraries you can split the date in to parts and pass them to Date(year, month, day) constructor.
var myDate = '09-06-2017';
var myDateParts = myDate.split('-');
var selectedDate = new Date(myDateParts[2], myDateParts[0] - 1, myDateParts[1]);
console.log(selectedDate);
This question already has answers here:
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
Closed 7 years ago.
How can I change the output to be more readable? from this 2015-04-25T00:00:00Z to 2015-04-2 00:00:00-00:00:00
I am using this (new Date()).toISOString()
Dates are very strange beast in javascript. If at all possible I would highly recommend using a date library like momentjs to handle this for you. Using moment this would be as simple as:
var formattedDate = moment().format('YYYY-MM-DD hh:mm:ss')
This is one way to handle it in JavaScript. You get the Date and then the various parts. Then you concatenate them together into a variable to use as you wish.
var d = new Date();
var dy = d.getDate();
var dmth = d.getMonth();
var dyr = d.getFullYear();
var dFullDate = dyr+ '/' +dmth+ '/' +dy;
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();