This question already has answers here:
Get difference between 2 dates in JavaScript? [duplicate]
(5 answers)
Closed 8 years ago.
I have two dates in format: Y-m-d H:i:s
How I can calculate time beetweeb two dates:
data.endTime - data.startTime
The best way is like #Paul S. say, convert them into Date, and then yo can make something like this:
var resultInMs = date_end.getTime() - date_start.getTime();
Hope it works for you.
PD: Here is something that can help you http://www.w3schools.com/jsref/jsref_obj_date.asp
PD2: Like #James Thorpe says Mozilla Docs is also a very good source of information (Even better): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
Related
This question already has answers here:
Convert UTC Epoch to local date
(16 answers)
Closed 4 months ago.
i want to print this date: 21.10.2022
with following timestamp: 1666344563
my code:
new Date(parseInt(element.createdAt)).toLocaleDateString())
FYI element.createdAt = '1666344563'
it prints in chrome: 20.01.1970
can someone explain whats happening here?
JavaScript uses miliseconds timestamps. Your value is made from seconds. To make it work just multiply this value by 1000 like so:
new Date(parseInt(element.createdAt) * 1000).toLocaleDateString())
#EDIT:
Thanks #NineBerry for noticing. Multiply after parsing.
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:
How to calculate number of days between two dates?
(42 answers)
Closed 6 years ago.
Today is 3/26/2016. I want to Print out how many days are until 4/05/2016 ex.
The answer is 10 days.
I want to do this with Javascript Code.
With standard javascript or with the use of a Library or Framework.
Just to calculate the Difference between two dates.
Easy, no too complicated.
InsertDateHere ------- 3/26/2016
InsertDateHere ------ 4/05/2016
ResultHere -------------- AnswerHere ("10" ex.)
if anyone can help, please Do. Thanks a lot People!
Moment.js is a helpful library that can also handle timezones and DST. The method that you're looking for is difference.
If this is the only date-handling that you'll be doing, it's likely not worth it to pull in a library. You can handle this easily with a simple function.
var diffInDays = function diffInDays (dateA, dateB) {
var difference = dateA - dateB;
return Math.floor(Math.abs(difference / (1000*60*60*24)));
};
diffInDays(Date.parse('3/26/2016'), Date.parse('4/05/2016'));
// > 10
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"
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.