Here's an issue I'm getting setting a date with Javascript on Chrome
"17-09-2019"
var [day, month, year] = date.split('-');
undefined
day
"17"
month
"09"
year
"2019"
var set_date = new Date(parseInt(year), parseInt(month), parseInt(day));
undefined
console.log(set_date);
VM5577:1 Thu Oct 17 2019 00:00:00 GMT+0300 (East Africa Time)
undefined
So I have a date 17-09-2019 and point here is 09 is September but when I set the date I get back October.
Been scratching my head over this one for a while now. Not sure what's going on.
The months in javascript starts from 0. You just need to subtract 1 to your parseInt(month):
var date = "17-09-2019";
var [day, month, year] = date.split('-');
console.log(`${day}-${month}-${year}`)
var set_date = new Date(parseInt(year), parseInt(month) - 1, parseInt(day));
console.log(set_date);
Set out this , month starting from index 0
var date = "17-09-2019"
var [day, month, year] = date.split('-');
month = month - 1;
var set_date = new Date(parseInt(year), parseInt(month), parseInt(day));
console.log(set_date); //Tue Sep 17 2019 00:00:00 GMT+0530 (India Standard Time)
Related
Hey folks running into a problem with dates that I would love an extra set of eyes on. Any help would be appreciated.
Problem: I have a future event which will occur on a certain date, when I subtract the future event date from the current date I get a year that is in the past and an extra day.
let evenDate = "12 08 2019, 12:00 am";
let eventFormat = "MM DD YYYY, h:mm a";
const futureEvent = moment(evenDate, eventFormat).utc();
const now = moment().utc();
const timeDiff = moment(futureEvent - now).utc(); // also tried futureEvent.diff(now)
let daysLeft: moment(timeDiff).format('D') - 1; // have to subtract 1
console.log('futureEvent:', futureEvent);
// Sun Dec 08 2019 00:00:00 GMT-0700 (Mountain Standard Time)
console.log('now:', now);
// Wed Dec 04 2019 10:18:26 GMT-0700 (Mountain Standard Time)
console.log('timeDiff:', timeDiff);
// Sun Jan 04 1970 06:41:33 GMT-0700 (Mountain Standard Time)
let evenDate = "12 08 2019, 12:00 am";
let eventFormat = "MM DD YYYY, h:mm a";
const futureEvent = moment(evenDate, eventFormat);
const now = moment();
const timeDiff = moment.duration(futureEvent.diff(now));
let daysLeft = timeDiff.asDays();
const disp = document.getElementById("display");
disp.innerText = daysLeft.toFixed(); // 3.0123
console.log("futureEvent:", futureEvent.toString());
Same code i just get rid of .utc()s. used .duration() and for getting dates used .asDays().
Pen
I am trying UTCString to above format. I can able to convert, problem is after conversion it shows a day before.
var newDate = this.getCellDate(target);
console.log(newDate); --> Dec 05 2014 00:00:00 GMT+0800 (Malay Peninsula Standard Time)
cstDate = newDate.toISOString();
console.log(cstDate); -- > 2014-12-04 --- > **Expected --> 2014-12-05**
Use Date.UTC() method
var now = new Date(), // my date Thu Dec 04 2014 13:02:15 GMT+0300 (RTZ 2 (зима))
year = now.getFullYear(),
month = now.getMonth(),
day = now.getDay(),
hours = now.getHours(),
minutes = now.getMinutes(),
utcDate;
utcDate = new Date(Date.UTC(year, month, day, hours, minutes)); // Thu Dec 04 2014 16:02:00 GMT+0300 (RTZ 2 (зима))
Ext.Msg.alert('UTC Date', Ext.Date.format(utcDate, 'Y-m-d'));
Look at this "Thu Dec 04 2014 16:02:00" - i got utc time(+3 hours)
Fiddle example
Yeah i got the solution. I should not toISOString. instead i need to use toLocaleDateString
custdate = newDate.toLocaleDateString();
dueDate= custdate.split("/").reverse().join("-");
I don't understand why this code doesn't work. Solutions on SO mention that doing date.getDate()+1 should add a day but in my case it adds one month and two days?
var year = 2014;
var month = 3;
var day = 31;
// creating an actual date
requestedDate = new Date(year, month - 1, day);
console.debug(requestedDate.toString());
// outputs "Mon Mar 31 2014 00:00:00 GMT+0200 (CEST)"
var d = new Date();
d.setDate(requestedDate.getDate()+1);
console.debug(d.toString());
// outputs "Fri May 02 2014 11:04:52 GMT+0200 (CEST)"
You're not setting the second date to the same as the first date.
In the first new Date() you're setting the date to 31. march.
The second new Date() sets the date to today, 1. april.
31 + 1 = 32, and 1. april plus 32 days should be 2. may.
var year = 2014;
var month = 3;
var day = 31;
// creating an actual date
requestedDate = new Date(year, month - 1, day);
console.debug(requestedDate.toString());
var d = new Date(year, month - 1, day); // set the date to the same
d.setDate(requestedDate.getDate()+1);
console.debug(d.toString());
FIDDLE
I have two variables namely
date1 = Mon Nov 25 2013 00:00:00 GMT+0530 (IST)
date2 = Mon Nov 25 2013 14:13:55 GMT+0530 (IST)
When I compare the two dates I get that date2 is greater which I need is correct. But I do not want to check the time part of the two dates I have. How could I get the date part alone from these two dates and compare it?
var today = new Date(); //Mon Nov 25 2013 14:13:55 GMT+0530 (IST)
d = new Date(my_value); //Mon Nov 25 2013 00:00:00 GMT+0530 (IST)
if(d>=today){ //I need to check the date parts alone.
alert(d is greater than or equal to current date);
}
Try clearing the time using Date.setHours:
dateObj.setHours(hoursValue[, minutesValue[, secondsValue[, msValue]]])
Example Code:
var today = new Date();
today.setHours(0, 0, 0, 0);
d = new Date(my_value);
d.setHours(0, 0, 0, 0);
if(d >= today){
alert(d is greater than or equal to current date);
}
The best way would be to modify the accepted answer's if statement as follows
if(d.setHours(0,0,0,0) >= today.setHours(0,0,0,0))
In this way, you can easily check for equality as well because the return type for setHours() is integer.
Try:
var today = new Date(); //Mon Nov 25 2013 14:13:55 GMT+0530 (IST)
var d = new Date(my_value); //Mon Nov 25 2013 00:00:00 GMT+0530 (IST)
var todayDateOnly = new Date(today.getFullYear(),today.getMonth(),today.getDate()); //This will write a Date with time set to 00:00:00 so you kind of have date only
var dDateOnly = new Date(d.getFullYear(),d.getMonth(),d.getDate());
if(dDateOnly>=todayDateOnly){
alert(d is greater than or equal to current date);
}
var StartDate = $("#StartDate").val();
var EndDate = $("#EndDate").val();
if ((( EndDate - StartDate)/ (86400000*7))<0)
{
alert("Start Date Must Be Earlier Than End Date"); $("#StartDate").focus();
error = true;
return false;
}
How can I obtain the last day of the month with the timestamp being 11:59:59 PM?
function LastDayOfMonth(Year, Month) {
return new Date((new Date(Year, Month, 1)) - 1);
}
console.log(LastDayOfMonth(2009, 11))
Example:
> LastDayOfMonth(2009, 11)
Mon Nov 30 2009 23:59:59 GMT+0100 (CET)
This will give you last day of current month.
var t= new Date();
alert(new Date(t.getFullYear(), t.getMonth() + 1, 0, 23, 59, 59));
var d = new Date();
console.log(d);
d.setMonth(d.getMonth() + 1); // set month as next month
console.log(d);
d.setDate(0); // get the last day of previous month
console.log(d);
Here is output from the code above:
Thu Oct 03 2013 11:34:59 GMT+0100 (GMT Daylight Time)
Sun Nov 03 2013 11:34:59 GMT+0000 (GMT Standard Time)
Thu Oct 31 2013 11:34:59 GMT+0000 (GMT Standard Time)
var d = new Date();
m = d.getMonth(); //current month
y = d.getFullYear(); //current year
alert(new Date(y,m,1)); //this is first day of current month
alert(new Date(y,m+1,0)); //this is last day of current month
var month = 1; // 1 for January
var d = new Date(2015, month, 0);
console.log(d); // last day in January
Sometimes all you have is a text version of the current month, ie: April 2017.
//first and last of the current month
var current_month = "April 2017";
var arrMonth = current_month.split(" ");
var first_day = new Date(arrMonth[0] + " 1 " + arrMonth[1]);
//even though I already have the values, I'm using date functions to get year and month
//because month is zero-based
var last_day = new Date(first_day.getFullYear(), first_day.getMonth() + 1, 0, 23, 59, 59);
//use moment,js to format
var start = moment(first_day).format("YYYY-MM-DD");
var end = moment(last_day).format("YYYY-MM-DD");
Last day of the month
now = new Date
lastDayOfTheMonth = new Date(1900+now.getYear(), now.getMonth()+1, 0)
Most of these answers are missing one thing or another. After playing with most of them I came up with the following that gives you the last possible millisecond of the month.
let testDate = new Date();
console.log(getMonthEnd(testDate));
function getMonthEnd(value) {
return new Date(value.getFullYear(), value.getMonth() + 1, 0, 23, 59, 59, 999);
}
Probably missing something in this one too but seems to cover all my requirements.
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DATE, cal.getActualMaximum(Calendar.DATE));
Date lastDayOfMonth = cal.getTime();
Do not forget month started with 0 so +1 in month too.
let enddayofmonth = new Date(year, month, 0).getDate();