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
Related
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);
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);
This question already has answers here:
How to subtract days from a plain Date?
(36 answers)
Closed 5 years ago.
I am trying to populate two date input fields, one with today's date and one with the date 30 days ago(last month).
I am getting an error in my console: priordate.getDate is not a function
Here is my code, not sure what I am doing wrong:
//today's date
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1;//January is 0, so always add + 1
var yyyy = today.getFullYear();
if(dd<10){dd='0'+dd};
if(mm<10){mm='0'+mm};
today = yyyy+'-'+mm+'-'+dd;
//30 days ago
var beforedate = new Date();
var priordate = new Date().setDate(beforedate.getDate()-30);
var dd2 = priordate.getDate();
var mm2 = priordate.getMonth()+1;//January is 0, so always add + 1
var yyyy2 = priordate.getFullYear();
if(dd2<10){dd2='0'+dd2};
if(mm2<10){mm2='0'+mm2};
var datefrommonthago = yyyy2+'-'+mm2+'-'+dd2;
// Set inputs with the date variables:
$("#fromdate").val(datefrommonthago);
$("#todate").val(today);
You'll instead want to use:
var priordate = new Date(new Date().setDate(beforedate.getDate()-30));
if you want it on one line. By using:
new Date().setDate(beforedate.getDate()-30);
you're returning the time since epoch (a number, not a date) and assigning it to priordate, but it's not a Date anymore and so does not have the getDate function.
This is because setDate() returns a number, not a Date Object.
In any I case I'd strongly recommend using momentjs or date-fns as the internal Date object in JavaScript is broken in all kind of ways. See this talk: https://www.youtube.com/watch?v=aVuor-VAWTI
Change this:
var priordate = new Date().setDate(beforedate.getDate()-30);
to this:
var priordate = new Date();
priordate.setDate(beforedate.getDate()-30);
i want to get the value of the day format from new Date()(current date) in my angularjs projet. I try this code in my javascript file:
var today = (new Date()).toISOString();
console.log(today.getDay());
when running my code, i get this message error :
TypeError: today.getDay is not a function
however there are many solutions with this syntax.
How can i fix it please. Any help is appreciated
Use getDay on the Date object not on the ISO string:
var today = (new Date()).getDay();
getDay returns a value from 0(Sunday) to 6(Saturday).
If you want current date and day according to your timezone then ->
var today = new Date().getDay() // 0(Sunday) to 6(Saturday).
var currentDate = new Date().getDate()
If you want current date and day according to UTC timezone then ->
var today = new Date().getUTCDay() // 0(Sunday) to 6(Saturday).
var currentDate = new Date().getUTCDate()
You can get date by using below code
let dayno = new Date(this.date.getFullYear(), this.date.getMonth() ,20).getDay();<br>
if date is 20-11-2019 then Day No is :3
I've used Date Format 1.2.3 to format my time into yyyy-mm-dd hh:MM:ss and used it for a startTime variable. How can add 4 hours to the startTime string and use it for a new variable (endTime)?
You can try to parse it as Date, add 4 hours and then format it into text again.
var d = new Date(startTime);
d.setHours(d.getHours() + 4);
var endTime = // Format d variable as you like
Parse it as a date then add 4 to the hours
Here's an example with the date object
var today = new Date();
today.setHours(today.getHours() + 4);
Javascript will automatically update the day/month/year if required