Javascript date undefined error - javascript

this is my code and I'm getting "Uncaught TypeError: undefined is not a function" what am i doing wrong?
var myDate = new Date().setDate(17);
document.getElementById("result").innerHTML = myDate.getDate();

setDate modifies the object it is called on and returns undefined. If you want to make somethingelse refer to the date that today referred to, but changing the day, you could copy today and then change somethingelse:
var today = new Date();
var somethingelse = new Date(today.getTime());
somethingelse.setDate(17);
document.getElementById("result").innerHTML = somethingelse.getDate();
Of course, if you didn’t care about preserving what was in today, you could certainly modify that without creating a copy.
var date = new Date();
date.setDate(17);
document.getElementById("result").innerHTML = date.getDate();

This much is enough: You can take the same today object and set and get date for that object.
var today = new Date();
today.setDate(17);
document.getElementById("result").innerHTML = today.getDate();
Fiddle

Related

currentDate.diff() is not a function error in moment.js

const currentDate = moment(new Date()).format('DD/MM/YYYY'); //03/01/2022
var days_diff = currentDate.diff(returnDate,'days'); // returnDate = 08/12/2021
console.log(days_diff)
Error:
Uncaught TypeError: currentDate.diff is not a function
I am trying to get the days difference between the current date and the return date but it is giving me an error currentDate.diff is not a function
Please solve this error.
This is because .format('DD/MM/YYYY') outputs a string, which is not going to have the Moment functions available to it. Instead do this:
const currentDate = moment(new Date('03/01/2022'));
const returnDate = moment(new Date('08/12/2021'));
var days_diff = currentDate.diff(returnDate,'days');
console.log(days_diff)
Maintain the Moment Date object throughout your operations. Formatting it early just makes it a string.

new Date() gives me an advanced date when trying to set it to input

I'm trying to set the date to my datepicker with the following code.
document.getElementById('selFecha').valueAsDate = new Date();
<input type='date' id='selFecha'>
But it gives me the date of tomorrow. I'd say there's nothing wrong with the local date of my pc because I sended new Date() to the console and gave me the current date.
I solved it with the following:
var fecha = new Date;
var d = fecha.getDate();
var m = fecha.getMonth()+1;
var y = fecha.getFullYear();
if(d<10){
d='0'+d;
}
if(m<10){
m='0'+m;
}
var hoy = y+"-"+m+"-"+d;
document.getElementById('selFecha').value = hoy;
But I don't like it. What could be happening with the first code?
My timezone is -6. I tested in Chrome and Firefox.
Well, now this works. Thanks to ecg8 who post me a blog where it's explained.
var f = new Date();
document.getElementById('selFecha').valueAsDate = new Date(f.getFullYear(), f.getMonth(), f.getDate(), 12);
<input type='date' id='selFecha'>
The input date from HTML doesn't read the time zone, so you have to send the current day as argument to new Date() with help of another new Date() runned before.
The 12 is to especify an hour of the day (in order to avoid time-zone issues, apparently).
.valueAsDate() will always give you the GMT date by default.
https://austinfrance.wordpress.com/2012/07/09/html5-date-input-field-and-valueasdate-timezone-gotcha-3/

Formatting dates when getting days between 2 dates with JQuery/Javascript

I am trying to calculate the days between 2 dates and it is working as far as I can tell but I keep getting stupidly high numbers which clearly isn't right, I have a feeling this is the way my dates are set out. my dates are set out as dd/mm/yyyy and this is the code I am using:
var diff = new Date(end_date - start_date);
var days = diff/1000/60/60/24;
console.log("diff=>"+days);
This is the question I used to get the answer:
JavaScript date difference Days
When it writes to the console this is the result I get:
diff=>17301.95833332176
I have had a play with the code, although I have not used HTML, i set the vars statically below.
var end_date = new Date("May 25, 2017");
var start_date = new Date("May 23, 2017");
var diff = new Date(end_date - start_date);
var days = diff/1000/60/60/24;
console.log("diff=>"+days);
I have also checked it with a 3 value date format
var end_date = new Date(2017,4,25);
var start_date = new Date(2017,4,23);
var diff = new Date(end_date - start_date);
var days = diff/1000/60/60/24;
console.log("diff=>"+days);
I manage to get an output of 2. Which is what i expected. The code you supplied looks ok to me. Maybe look at the HTML to check that the values being passed are in the correct format.
Jquery datepicker may be of help to you here.
You could also use moment: https://momentjs.com
var moment = require('moment');
var start_moment = moment(start_date);
var end_moment = moment(end_date);
var days = start_moment.diff(end_moment, 'days');
console.log("diff=>" + days);
You can also get weeks, months etc. with this method
Easy solution, is to use countBtw
var { date } = require('aleppo')
//..
date.countBtw('all', date1, date2)

How do I get the Monday of the current week?

(function () {
var date = new Date().toISOString().substring(0, 10),
field = document.querySelector('#date');
var day = new Date(date);
var getMonday = day.getDay(),
diff = date.getDate() - getMonday + (getMonday == 0 ? -6:1);
field.value = new Date(date.setDate(diff));
console.log(date);
})();
I am trying to get the Monday of the current date.
I keep getting errors about it and not sure how to resolve it
TypeError: date.getDate is not a function
at index.html:394
at index.html:398
(anonymous) # index.html:394
(anonymous) # index.html:398
Post of so called Duplicate only asks for the how to get the date. My question has similar code but I am getting error messages that was never addressed in the question
You transform the date to string in the first line:
date = new Date().toISOString().substring(0, 10) that is what causes the error... date is no longer a Date object.
--- EDIT: Solution
I would suggest you to either declare an additional variable for any of your later use as ISO string, or to make the conversion after only when output:
For this, I'd suggest add your format to the Date object's prototype
Date.prototype.myFormat = function() {
return this.toISOString().substring(0, 10);
}
Update your initial code like this:
var date = new Date(),
str_date=date.toISOString().substring(0, 10),
field = document.querySelector('#date'),
day = new Date(date),
getMonday = day.getDay(),
diff = date.getDate() - getMonday + (getMonday == 0 ? -6:1);
console.log(date);
console.log(str_date);
console.log(new Date(date.setDate(diff)));
console.log(new Date(date.setDate(diff)).myFormat());
//Now you can update your field as needed with date or string value
field.value = new Date(date.setDate(diff));
field.value = new Date(date.setDate(diff)).myFormat();
If you need it in more places make the getMonday also a function...
Happy coding,
Codrut

Getting "Invalid Date" when passing dynamic date in IE 11 only in Javascript

Creating date using dynamically generated date string throws invalid date message only in IE11.
Example
var today = new Date();
var ag_endDate = new Date(today).toLocaleDateString('en-US');
var final = new Date(ag_endDate);
console.log(final);
try this
var dateObj = new Date();
var usFormatDate = dateObj.toLocaleDateString('en-US');
console.log( usFormatDate );
sorry, my bad! date constructor does take date Object as parameter.
However, if your intention is to simply get the formatted date in en-US locale, then you don't have to create another date object for the same.

Categories

Resources