I have a date coming over from a database. The response is via django python (although that shouldn't matter since json is json) but context. I then get the current date in javascript. the goal is to take todays date and the one sent over by the server and find out how many days apart.
example today and tomorrow is 1 day away
I also have a screen shot of the console.
What am I doing wrong.
const today = new Date();
console.log('today');
console.log(today);
const creationdate = this.creatorobject.creationdate;
console.log(creationdate);
const dayssince = today - creationdate;
console.log('here is days since');
console.log(dayssince);
First i would have a variable to convert the JSON obj date to date format by simply passing it as a string
var creationDate = new Date(this.creatorobject.creationdate);
ex : const d = new Date("2015-03-25");
then next find the difference between the 2 days ie: today - creationDate..
If they are 2 date formats then you get milliseconds. Convert milliSeconds to days and round it up to get a date.
var today = new Date();
var creationDate = new Date("2022-01-01");
var diff = today - creationDate;
const convertedDate = diff / (1000*60*60*24);
console.log(convertedDate);
Related
const today = new Date()
const tomorrow = new Date(today)
const newDate = tomorrow.setDate(tomorrow.getDate() + 2)
console.log(newDate.toLocaleDateString('en-us'))
I'm trying to get next 2 days date with mm/dd/yyyy format, but getting issues.
I tried with following code:
console.log(new Date().toLocaleDateString('en-US'));
Example: today's date >> 6/1/2022
Expected result : 6/3/2022
Your problem comes from the fact, .setDate does not return a Date Object, but instead modifies the Date Object you call it on.
This means, tomorrow will be modified by calling .setDate.
Just change your code to the following, to get the expected result:
const today = new Date()
const tomorrow = new Date(today)
tomorrow.setDate(tomorrow.getDate() + 2)
console.log("today:", today.toLocaleDateString('en-US'))
console.log("in two days:", tomorrow.toLocaleDateString('en-us'))
I have two columns in the PostgreSQL table(start_date(in date format) and cancellation_date(in timestamp format)). One is in date format and another is in timestamp format. I want to get the difference between two values in the number of days. I am sending the data in javascript.
How to calculate the difference in days in javascript? for example, one value is 2010-10-14. the second value is 2010-10-16 04:05:06. the difference will be 3 days.
My problem is that I get these two values in API response as PostgreSQL(date and timestamp) format. And a simple difference is not working(start_date-cancellation_date) in javascript.
Javascript date objects can't be compared until they're converted to milliseconds.
var now = new Date(); // create new date - today
var then = new Date(now.getDate()+5); // create new date that's now + 5 days (bad variable name, sry)
var nowAdj = now.getTime(); // now adjusted to milliseconds
var thenAdj = then.getTime(); // then adjusted to milliseconds
var differenceInDays = Math.round((thenAdj - nowAdj)/(1000*60*60*24)) // Subtract now from then and multiply the results by the magic formula to return milliseconds to days.
Edit: Cleaned up a bit, "then" is still a terrible variable name ;)
You can convert the dates into new date objects and calculate the difference as follows:
var date1 = new Date("2010-10-14");
var date2 = new Date("2010-10-16 04:05:06");
var timeDiff = Math.abs(date2.getTime() - date1.getTime());
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));
console.log(diffDays);
I have a variable containing timestamp ( in seconds generated with php ), I used following code to create date object using javascript
var newdate = new Date(arrivalDate);
then used following code to get formated date in utc
var tempdate = newdate.getUTCFullYear()+'-'+('0' + (newdate.getUTCMonth()+1)).slice(-2)+'-'+('0' + newdate.getUTCDate()).slice(-2);
It returns me a 1 day before date, not sure how to get utc date from a timestamp.
var newdate = new Date(arrivalDate);
newdate.toUTCString();
I have 2 DateTime field in a form, and I want the difference between these 2 fields in minute.
I tried to parse DateTime into Date but it's not working :
<script>
$(document).ready(function () {
$("#mybundle_evenement_button").click(function () {
var field1 = $("#mybundle_evenement_debut").val();
var field2 = $("#mybundle_evenement_fin").val();
var date1 = new Date(field1);
var date2 = new Date(field2);
alert(date1);
});
});
</script>
If I alert() date1, it shows Invalid Date.
But if I alert() field1, it shows 15/09/2017 13:32 (format is : days/months/year hour:minutes).
Is it possible that new Date(field1) isn't working because of the format ?
I know that if I succeed to parse DateTime into Date, it'll be easy to have the difference in minutes, but I don't understand why it says Invalid Date.
dd/MM/yyyy HH:mm isn't a valid date format for Date.parse()
You have to format your date to a valid Date Time String Format, for example:
var field1 = $("#mybundle_evenement_debut").val();
var ISODate1 = field1.replace(/(\d+)\/(\d+)\/(\d+)/, "$3-$2-$1")
var date1 = new Date(ISODate1);
alert(date1) // => Fri Sep 15 2017 13:32:00 ...
The problem is about the format you are getting the date from the field. new Date() don't accepts this format. I think the best is to parse the string yourself. If the format is always the same just use new Date(year, month, day, hours, minutes, seconds, milliseconds).
var day = field.splice(0,2); field.splice(0,1);
var month = field.splice(0,2); field.splice(0,1);
var year = field.splice(0,2); field.splice(0,1);
var hour = field.splice(0,2); field.splice(0,1);
var minute = field.splice(0,2);
It's depend on your browser. I'll suggest to use the standard format is '2013/12/09 10:00'.
Okay! come to the point. You need to manually format the date from my latest answer regarding this same kind of issue. Please take a look at this link : Stange javascript Date behaviour on particular dates
And you could try this below code for getting the date difference in minutes.
var startTime = new Date('2013/12/09 10:00');
var endTime = new Date('2014/12/09 10:00');
var difference = endTime.getTime() - startTime.getTime();
var result = Math.round(difference / 60000);
alert(result);
I want to get the difference between tow dates in javascript but my problem is that the first date is the currenet date of the PC :
today = new Date();
and the other date is a text format date like this for example:
other_date = '15.11.2013';
I want the today date to be same format as other_date and then subtract them , How can I change the format of the today to make the same as other_date ? and How I can make them both as date format to subtract them and get the difference correctly???
I would simply use the split function to get the date parts of the date string:
var today = new Date();
var other_date = '15.11.2013';
var dateparts = other_date.split('.');
var otherDate = new Date(dateparts[2], dateparts[1]-1, dateparts[0]); // substract 1 month because month starting with 0
var difference = today.getTime() - otherDate.getTime(); // difference in ms