What's wrong on adding Days at this Date function? - javascript

This is my code:
var dat = new Date("24/03/2013");
dat.setDate(dat.getDate() + 7);
console.log(dat)
but it print Tue Jan 06 2015 00:00:00 GMT+0100?
The date is wrong: should be 31/03/2013 (and I'd like to print it in this format).

My browser (Chrome) prints "Invalid date", but apparently yours interprets the initializing date in mm/dd/yyyy format instead of dd/mm/yyyy. Therefore it thinks it's the 3rd day of the 24th month of 2013, which is January 3rd, 2015.
I'm not sure why it would print it as January 6th if you add 7 days to it.
The safest way is to give the numbers explicitly:
var dat = new Date( 2013, 2, 24 );

Change the format of your date to put the day after the month:
var dat = new Date("03/24/2013");
dat.setDate(dat.getDate() + 7);
console.log(dat)
For me this returns:
Sun Mar 31 2013 00:00:00 GMT+0000 (GMT Standard Time)

You should have to give the month number first. Then you'll get the correct answer
Try this code
var dat = new Date("03/24/2013");
dat.setDate(dat.getDate() + 7);
var curr_date = dat.getDate();
var curr_month = dat.getMonth() + 1; //Months are zero based
var curr_year = dat.getFullYear();
console.log(curr_date + "/" + curr_month + "/" + curr_year);

You should print out your date before you add seven. I suspect it's being set to the 3rd day of the 24th month in 2013, which equates to somewhere close to January 2015.
That's why you're getting a date well advanced from the current one. Why it's giving you the 6th of January rather than the 10th, I'm not sure, but you can probably fix it just by changing your input string to the US format of mm/dd/yyyy, or using a more explicit constructor that's not subject to misinterpretation:
var d = new Date(year, month, day, hours, minutes, seconds, milliseconds);

try this!!!
var aDate = new Date(2013,3,24);
aDate.setDate(aDate.getDate() + 7);
var dateString = aDate.getDate() + "-" + aDate.getMonth() + "-" + aDate.getFullYear();
alert(dateString);

Related

How to get date with specific time in javascript

I am trying to fetch 1st and last date and time of current month and I have to convert to ISOString.
I tried below but when I am converting ISO its reducing 1 day and after removing it coming proper date .
I have to get 1st and last day of month with time ..
This is what I want to have:
start datetime 2022-12-01T00:00:00.000Z
end datetime 2022-12-31T23:59:59.000Z
Here is my code:
var date = new Date();
var firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
var lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);
console.log(firstDay.toISOString())
console.log(lastDay.toISOString())
console.log(firstDay)
console.log(lastDay)
Result:
// ISO string:
2022-11-30T18:30:00.000Z
2022-12-30T18:30:00.000Z
// without ISO string:
VM4876:4 Thu Dec 01 2022 00:00:00 GMT+0530 (India Standard Time)
VM4876:5 Sat Dec 31 2022 00:00:00 GMT+0530 (India Standard Time)
You can use Date.UTC() to help you construct a date instance at the specified UTC datetime:
var firstDay = new Date(Date.UTC(y, m, 1));
var lastDay = new Date(Date.UTC(y, m + 1, 0, 23, 59, 59, 0));
Your date instances will now log the expected UTC time:
console.log(firstDay.toISOString())
console.log(lastDay.toISOString())
2022-12-01T00:00:00.000Z
2022-12-31T23:59:59.000Z
let currentDate = new Date();
let cDay = currentDate.getDate();
let cMonth = currentDate.getMonth() + 1;
let cYear = currentDate.getFullYear();
console.log("<b>" + cDay + "/" + cMonth + "/" + cYear + "</b>");
thats how you get date and time in js
i got it from here : https://www.w3docs.com/snippets/javascript/how-to-get-the-current-date-and-time-in-javascript.html
you might want to go over on how to do it.

Javascript Converting Ints to Time?

For a project app I'm making, a homework keeper, I need to be able to turn a number like 10 into a month like october, then do this with year, month, day, and time. Then I need to save it all in a date. How do I do this? I have been looking everywhere and cannot find how to do it.
It looks like you're looking for the Date() constructor.
var month = 10;
var day = 17;
var year = 2017;
var hour = 8;//Use the 24 hour clock for times in the PM
var minutes = 36;
var date = new Date(year, month-1, day, hour, minutes);//Outputs October 17th, 2017 at 8:36am in your local timezone
You have to subtract 1 from the month because January starts at 0, not 1.
Alternatively, I like to use moment.js for the flexibility depending on use, so you could instantiate it like this:
var date = moment(year + '-' + month + '-' + day + ' ' + hour + ':' + minutes, 'YYYY-M-D H:m');//Because you are using numbers/integers, none of them will have preceding zeroes
Take a look at the Date constructor - I think it doesn everything you need.
Your biggest gotcha is that months are 0-indexed so you'll actually turn 9 into October. Also beware that if you don't initialise Date with anything, it'll assume you mean now.
var date = new Date();
date; // -> Tue Oct 17 2017 13:34:49 GMT+0100 (GMT Daylight Time)
date.setMonth(10); // -> 1510925689970
date; // -> Fri Nov 17 2017 13:34:49 GMT+0000 (GMT Standard Time)

Invalid date being returned

using Mirth Connect.
I'm trying to get the current date and add 2 days to it.
it is currently 8:10am Eastern Time 3/24/2016
var startDate
var currentDay = DateUtil.getCurrentDate('F');
var currentDate = new Date(DateUtil.getCurrentDate('yyyy-MM-dd'));
logger.info("Current: " + currentDate);
currentDate.setDate(currentDate.getDate()+2);
startDate = DateUtil.formatDate('yyyy-MM-dd', currentDate);
logger.info("+2: " + startDate);
I get this in return
[2016-03-24 08:10:09,694] INFO (db-connector:?): Current: Wed Mar 23 2016 20:00:00 GMT-0400 (EDT) - It's not wednesday nor is the time correct
[2016-03-24 08:10:09,694] INFO (db-connector:?): +2: 2016-03-25
- This should display Saturdays 2016-03-26 since i'm adding +2 days to the current time.
var currentDay = DateUtil.getCurrentDate('F'); however returns 4 so it is thursday.
I have switched to the following which solved my issue.
var currentDTTM = new java.util.Calendar.getInstance();
currentDTTM.add(java.util.Calendar.DATE, 2);
var strCurrentDTTM = new java.text.SimpleDateFormat("yyyy-MM-dd").format(currentDTTM.getTime());

Date comparison getting error

we stuck date comparison, we tried the below code. EndDate value is related to jan 21, 2016 but in alert showing showing Fri Sep 01 2017 10:10:10 GMT+0530 (India Standard Time), could you please share me your ideas
var endDateVal = "21/01/2016 10:10:10".replace(/-/gi, "/");
alert(new Date(endDateVal));
if (new Date(endDateVal) > new Date()) {
alert("Last end date should be minor than today");
}
Fiddle
The date is formatted incorrectly. The date needs to be an ISO 8601 or IETF-compliant RFC 2822 formatted date, like:
2016-01-21T10:10:10+05:30
To format the date you have you could do some thing like:
var dateVars = "21/01/2016 10:10:10".match(/\d+/g);
var reformattedDate = dateVars[2] + '-' + dateVars[1] + '-' + dateVars[0] + 'T' + dateVars[3] + ':' + dateVars[4] + ':' + dateVars[5] + '+05:30';
new Date(reformattedDate);
First, new Date("21/01/2016 10:10:10") returns Invalid Date. Default format for Date is mm/dd/yyyy and not dd/mm/yyyy.
Second, when comparing dates, you should use date.getTime() instead.
Following is a sample code.
var endDateVal = "02/21/2016 10:10:10";
var d1 = new Date(endDateVal)
var d2 = new Date();
console.log(d1, d2)
if (+d1 > +d2) {
alert("Last end date should be minor than today");
}
You need to swap your day/month around to 01/21/2016 10:10:10.
Also, Im not sure why you are using .replace(/-/gi, "/"); as this is replacing a - with / where your date does not have any -.

How To add number of month into the given date in javascript?

I want to add month into the select date by the user.
startdate=document.getElementById("jscal_field_coverstartdate").value;
now I want to add 11 month from the above startdate. How to do that.
date format = 2013-12-01
Without the date format it is difficult to tell, however you can try like this
add11Months = function (date) {
var splitDate = date.split("-");
var newDate = new Date(splitDate[0], splitDate[1] - 1, splitDate[2]);
newDate.setMonth(newDate.getMonth() + 11);
splitDate[2] = newDate.getDate();
splitDate[1] = newDate.getMonth() + 1;
splitDate[0] = newDate.getFullYear();
return startdate = splitDate.join("-");
}
var startdate = add11Months("2013-12-01");
alert(startdate)
JSFiddle
If your startdate is in correct date format you can try using moment.js or Date object in javascript.
In Javascript, it can be achieved as follow:
var date = new Date("2013-12-01");
console.log(date);
//output: Sun Dec 01 2013 05:30:00 GMT+0530 (India Standard Time)
var newdate = date.setDate(date.getDate()+(11*30));
console.log(new Date(newdate));
// output: Mon Oct 27 2014 05:30:00 GMT+0530 (India Standard Time)
In above lines, I have used 30 days per month as default. So you will get exact 11 month but little deviation in date. Is this what you want ? You can play around this likewise. I hope it help :)
For more about Date you can visit to MDN.
You can do it like this:
var noOfMonths = 11
var startdate = document.getElementById("jscal_field_coverstartdate").value;
startdate.setMonth(startdate.getMonth() + noOfMonths)
Try this:
baseDate.setMonth(2);
baseDate.setDate(30);
noMonths = 11;
var sum = new Date(new Date(baseDate.getTime()).setMonth(baseDate.getMonth() + noMonths);
if (sum.getDate() < baseDate.getDate()) { sum.setDate(0); }
var m = newDate.getDate();
var d = newDate.getMonth() + 1;
var yyyy = newDate.getFullYear();
return (yyyy+"-"+m+"-"+d);
Notes:
Adding months (like adding one month to January 31st) can overflow the days field and cause the month to increment (in this case you get a date in March). If you want to add months and then overflow the date then .setMonth(base_date.getMonth()+noMonths) works but that's rarely what people think of when they talk about incrementing months.
It handles cases where 29, 30 or 31 turned into 1, 2, or 3 by eliminating the overflow
Day of Month is NOT zero-indexed so .setDate(0) is last day of prior month.

Categories

Resources