Add two months on start date in CRM using Javascript - javascript

I have task to make expiry date by adding two months on start date.
I have found this code:
var startDate = Xrm.Page.getAttribute('new_startdate').getValue();
var expiryDate = new Date();
expiryDate.setDate(startDate.getDate()+60); //Add 60 days
var expiryField = Xrm.Page.getAttribute('new_expirydate').setValue(expiryDate);
I can see here how to add 60 days, but I need to add exactly 2 months. Can someone help me about this?

Try to do something like following:
var startDate = Xrm.Page.getAttribute('new_startdate').getValue();
startDate.setMonth(startDate.getMonth() + 2);
Xrm.Page.getAttribute('new_expirydate').setValue(startDate);

Related

How can I set minDate + 17hours in flatpickr

I'm creating a form with Fluentforms and it has inside a datetime field.
I would like to allow users to pick a date only 17 hours before the "departure".
So I need something like today + 17hours.
At first I've added today + 2 days
{
minDate: new Date().fp_incr(2)
}
But my client wants it + 17 hours.
I have seen that on flatpickr I should be able to use hourIncrement but i'm not really sure how to use it exactly.
I have tried also with this code but it's not working
function addHours(date, hours) {
const hoursToAdd = hours * 60 * 60 * 1000;
date.setTime(date.getTime() + hoursToAdd);
return date;
}
var newDate = addHours(new Date(),17);
Can someone please help me?

JavaScript - check if input date is within add 7 days from current yyyy/mm/dd date, NOT js date()

I have a form submit with 2 date inputs: share_start and share_end in yyyy-mm-dd format. I use JS to validate the input and want to check whether share_end date is within 7 days from the share_start date.
Now, the tricky bit is that I don't have a JS date() dates/timestamps, but only those input dates, but when trying to add on 7 days to the input in JS all I end up with an error since JS needs to operate with date(). I cannot use any external scripts like moment.js to help with this.
Does JS have some sort of in-built function like PHPs strtotime where I can just add + 7 days or something?
Thank you
// Form Submit Validation
function validateForm() {
var share_start = '2021-05-07';
var share_end = '2021-05-15';
var share_max = share_start.setDate(date.getDate() + 6);
if (share_end > share_max) {
alert("Share End Date cannot be more than 7 days from now");
return false;
}
}
At last figured it out.... Bloody JS date conversion is really a pain without libraries such moments.js
var date1 = '2021-01-01';
var date2 = '2021-01-08';
var diffTime = Math.abs(date2 - date1);
var diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
if (diffDays > 6) {
alert("Share cannot be longer than 6 days");
return false;
}
In my particular case, I am getting the date values from a variable, which I then calculate the difference in seconds, after which I convert those seconds to days. Followed by a simple if statement where I check if the value is greater than x days, and I am good to go.

Add minutes to current time, JavaScript [duplicate]

This question already has answers here:
How to add 30 minutes to a JavaScript Date object?
(29 answers)
Closed 3 years ago.
I would like to add 20 minutes to the current date. While browsing the messages already posted on this subject, I recovered a piece of code but I can not adapt it. Can you help me ?
// get the current date & time
var dateObj = Date.now();
// I do not understand what these values ​​are
dateObj += 1000 * 60 * 60 * 24 * 3;
// create a new Date object, using the adjusted time
dateObj = new Date(dateObj);
Create a prototype function on Date Object if you want to use it in various places as it will reduce redundancy of code.
Date.prototype.add20minutes = function(){
return this.setMinutes(this.getMinutes() + 20);
}
Now, you can simply call
var d = new Date();
d.add20minutes();
Use this piece of code
var date = new Date();
date.setMinutes(date.getMinutes()+20);
Don't know if setMinutes with values > 60 is defined or it works by accident. You can do it this way:
var current_ms = new Date().getTime();
var in20min = new Date(current_ms + (1000*60*20))
JavaScript Date object has a method called setMinutes
let d = new Date()
d.setMinutes(d.getMinutes() + 20)
In javascript when working with dates I like to use moment:
https://momentjs.com/
So you can do this:
moment().add(20, 'minutes');
Use this code:
var date = new Date();
var min = parseInt(date.getMinutes()+20);
date.setMinutes(min);

Javascript for time left in the day?

I understand how to work countdown timers and date timers to an extent (to a specified date i.e YYYY-MM-DD) but I'm working on a web development college project where I wish for one of my web pages (JSP file) to have a countdown timer with the number of seconds left in the day from when the web application launches.
The web page will also include an Ajax function where the user can click a button and a random motivational message will appear (this particular piece of code I know, but it's just to give you an idea of why I want this countdown timer).
Moment.js is a great library for date math. http://momentjs.com/
Using Moment, you could do a one liner like this.
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.8.4/moment.min.js"></script>
<script>
document.write(moment.duration(moment().add(1, 'day').startOf('day').diff(moment())).asSeconds())
</script>
or an easier to understand version:
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.8.4/moment.min.js"></script>
<script>
var now = moment(),
tomorrow = moment().add(1, 'day').startOf('day'),
difference = moment.duration(tomorrow.diff(now))
document.write(difference.asSeconds())
</script>
UPDATED
Simply compare the Date object you get from Date.now() with the date of tomorrow (which you create from the first date object, adding one day);
var actualTime = new Date(Date.now());
var endOfDay = new Date(actualTime.getFullYear(), actualTime.getMonth(), actualTime.getDate() + 1, 0, 0, 0);
var timeRemaining = endOfDay.getTime() - actualTime.getTime();
document.getElementById('timeRemaining').appendChild(document.createTextNode(timeRemaining / 1000 + " seconds or " + timeRemaining / 1000 / 60 / 60 + " hours"));
document.getElementById('dayProgression').value = 1 - (timeRemaining / 1000 / 60 / 60 / 24);
<span id="timeRemaining"></span>
<div>
<span>How much of the day has passed:</span>
<progress id="dayProgression" value="0"></progress>
</div>
Example JSFiddle
Try to use this Javascript code:
var year = 2015 , month = 5 , day = 15;
var date = new Date();
var enddate = new Date(year , month , day);
document.write( date - enddate );
You can edit this code for your site.

javascript increment date by month wise from given date

i am trying to make program . in which i have a text box with value of 2013-08-27 i want to increase this date by month wise up to 30 months
ex:-
2013-08-27
2013-09-27
2013-10-27
2013-11-27
2013-12-27
2014-01-27
2014-02-27
.........
2015-01-27
i already googled this topic ... but i am not getting any clue how to do it.. as for i get to this code
var myDate = new Date();
//add a day to the date
myDate.setDate(myDate.getDate() + 1);
but this is giving me reslut as 11,17 i want to increase year also by month wise up to 30 months from given date please give me any clue ... sorry for bad English
Try this:
var myDate = new Date();
for(var i =0;i<30; i++)
{
myDate.setMonth(myDate.getMonth() + 1);
console.log(myDate);
}

Categories