How to change the date format of ISOString? [duplicate] - javascript

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;

Related

how to get javascript Current Date [duplicate]

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

How to get the whole date time in a single line not separated [duplicate]

This question already has answers here:
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
Closed 4 years ago.
I need to get the date time in years,month,day,hours,minute,seconds,milliseconds in this format
201802281007475001
right now i managed to return this:
2018418112252159
This is my code:
var date = new Date();
blockid = JSON.stringify(date.getFullYear()) + JSON.stringify(date.getMonth()+1) + JSON.stringify(date.getDate()) + JSON.stringify(date.getHours()) + JSON.stringify(date.getMinutes()) + JSON.stringify(date.getSeconds()) + JSON.stringify(date.getMilliseconds())
I also need to put a zero in front of the month and the day in case it is minor than 10.
One simple way to this is to call toISOString on the date object and replace the non-required characters with blank:
var date = new Date();
console.log(date.toISOString().replace(/\D/g, ''));

JavaScript - Tell Date() to return in specific format [duplicate]

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

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

How should I instantiate this date in javascript? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Format Date in Javascript
I have this date:
var newDate = "/Date(1333609200000)/";
And what I need to do to it is:
var myDate = new Date(1333609200000);
I thought about just trimming off "/Date()/" and then getting just the number inside but I wasn't sure if that would be best here. Should I just simply regex the number out of that? Is there a better approach?
Notes: The date is being created by c#'s JavascriptSerializer and comes in the format newDate above. It cannot be changed in c# due to constraints.
You could do it like this:
var newDate = "/Date(1333609200000)/";
newDate= new Date(parseInt(newDate.match(/\d/g).join("")));

Categories