This question already has answers here:
What is the best way to convert date from JavaScript string in format YYYYMMDD to JavaScript date?
(2 answers)
Closed 9 years ago.
does anybody know How I could reverse the date that I have in javascript?
I have a created date of files from google drive which I pulled using the google drive api, however the date is reverse.
2014-02-25
I would like, 25-02-2014 (dd/mm/yyyy)
var date = resp.createdDate;
date = date.substring(0, date.length - 14)
This is my code, i trimmed the end off, because it came with time etc.
my output is currently : 2014-02-25
Im unsure how I could do this and im sure its simple!
> "2014-02-25".split('-').reverse().join('-')
"25-02-2014"
Related
This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Parsing a string to a date in JavaScript
(35 answers)
Closed 2 years ago.
I have a table where one of the columns is entered as CURRENT_TIMESTAMP.
I am pulling the data from the table and displaying it on my site. But can't figure out how to convert the date into a more reader friendly format.
Right now it looks like this: 2020-09-17T19:56:24.000Z (I also need to figure out how to convert to my timezone.)
But I want it to display like this: 09-17-2020 19:56 (would even prefer 07:56pm, but not critical)
I've seen a few examples where you can reformat it during the query. But all the examples I've seen are pulling just the date. I'm pulling everything. I'm fine with running a function on it after the query to convert... but I haven't been able to figure it out.
You could use a simple function using the built-in methods available to Date objects in js. Something like this is probably what you're looking for.
function formatDate(date){
const month = date.getUTCMonth()+1;
const day = date.getUTCDate();
const year = date.getUTCFullYear();
const hours = d.getUTCHours() > 12 ? d.getUTCHours() - 12 : d.getUTCHours();
const minutes = d.getUTCMinutes();
return `${month}-${day}-${year} ${hours}:${minutes}`}
This question already has answers here:
Is there a simple way to convert a decimal time (e.g. 1.074 minutes) into mm:ss format using moment.js?
(4 answers)
Closed 5 years ago.
I send TimeSpan object called time from back-end to front-end.
Here how the type looks in back-end:
I have this type on client side:
I need to display time variable only hours:minutes:seconds.
How can I convert the type above(i.e. time) to format above?
var x = new Date(1561000); //replace with TotalMilliSeconds in your case
var time = x.toUTCString().split(' ')[4]; //slit by space character
console.log(time);
This question already has answers here:
Why does Date.parse give incorrect results?
(11 answers)
Closed 5 years ago.
I'm working on a react app with firebase firestore for the db, the app stores a date field obtained from a datepicker in dd/mm/yyyy format as a string. I need to sort by dates and I'm converting dates to correctly sort by numbers with:
new Date(myDate).getTime()
But I'm getting NaN only on certain dates, for example:
01/12/2017 // 1484190000000
02/11/2017 // 1486782000000
22/08/2017 // NaN
Any idea???
var dateArr = '22/08/2017'.split('/');
new Date(dateArr[2], --dateArr[1], dateArr[0]).getTime();
Try this. 22/08/2017 - wrong format, need 08/22/2017
This question already has answers here:
Time not working as expected using moment.js
(2 answers)
Closed 6 years ago.
I am using full calendar and what should be very basic "dayClick" I am having trouble getting the date I clicked as a string.
dayClick: function(d){
var thisDay = d._d;
console.log(thisDay);
console.log(moment(thisDay).format('yyyy-mm-dd'));
console.log(thisDay.toString());
},
produces three very strange results. The 1st one I get. it is the date from the day object that I clicked on, which is correct, the second one is the result of trying to let momentJs format the date. the third is just using js toString function help out but it seems to change the date from the 4th to the 3rd.
What is going on here?
thisDay.toString() is converting it to your local time instead of showing the UTC date.
You can use thisDay.toISOString() instead.
This question already has answers here:
Closed 13 years ago.
Possible Duplicate:
How can I convert datetime microformat to local time in javascript?
Im writing up an ajax application where i have to interpret this date "2009-09-16T11:10:00" and output another string to something more readable.
That's the ISO 8601 date format. There's an example here. If that doesn't suit your needs then a quick google search should help.
No, there isn't a built-in function for doing that. You'd have to parse it yourself. Maybe something like this:
var s = "2009-09-16T11:10:00";
var tokens = s.split(/[\-T:]/);
var date = new Date(tokens[0], tokens[1] - 1, tokens[2],
tokens[3], tokens[4], tokens[5], 0);
Then access the date string with:
alert(date.toString());
Try this js library:
http://www.datejs.com
Pretty good and recognizes different date formats. You can also test your date right on the front page.