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?
Related
const cookieButton = document.getElementById('store-cookie')
cookieButton.addEventListener('click', e => {
const input = document.getElementById('fav-cookie').value
let date = new Date()
let minutes = 30;
date.setTime(date.getTime() + (minutes * 60 * 1000))
document.cookie = `favCookie=${input}; expires=${date.toTimeString()};`
})
I'm working on a coding problem meant to be only used in javaScript. It requires to make a cookie with the value of an input field (on a linked html), on pressing a button. The bonus for this question requires the cookie to expire after 30 minutes it's created. Currently, this code is just saving the
favCookie=input;
but it is not adding an expiration date. Any help would be appreciated!
Use toUTCString() instead.
const cookieButton = document.getElementById('store-cookie');
cookieButton.addEventListener('click', e => {
const input = document.getElementById('fav-cookie').value;
const date = new Date();
date.setTime(date.getTime() + (30* 60 * 1000));
document.cookie = `favCookie=${input}; expires=${date.toUTCString()};`;
});
Cookies are always tricky. I believe it is because your timestamp should be formatted like so:
2021-10-23T20:32:38.000Z
Instead you currently have set by toTimeString()
22:29:12 GMT+0200 (Central European Summer Time)
You could create 2 date variables, on the second use the setMinutes method to add 30mins to the current time then create a function to clear/change the value if and when the current date/time equals the new date.
var expire = new Date();
expire.setMinutes( expire.getMinutes() + 30 );
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.
I have written some Javascript code that spits out an estimated time frame.
For example, right now, it's giving me: 646 days, 5 hours, 13 minutes
Given that info, if I create a new date in Javascript like:
var d = new Date();
How do I use the info to project the date in the future?
For example, today is June 30, 2018 and 646 days from now is April 6, 2020. So I'd want to calculate that on page load each time, because the projected date is constantly changing.
You have two choices, calculate it in total and add the number (as seen in one of post above), else you can do part by part, meaning day part, time part etc, this way you can write your own mini prototype functions to account for all or say most types of date complexities such as daylight savings etc etc. Just to give a fair idea, start with the date.
To do so use Date.prototype.setDate(), Date.prototype.setHours, Date.prototype.setMinutes()
var projectEndDate = new Date();
projectEndDate.setDate(today.getDate()+646);
// use Date.prototype.setHours(), then
// Date.prototype.setMinutes() and so on
Just add the time to today:
new Date( Date.now() + ((646 * 24 + 5) * 60 + 13) * 60 * 1000)
Or if you want to consider that not every day has 24 hours:
const date = new Date;
date.setDate(date.getDate() + 646);
date.setHours(date.getHours() + 5);
date.setMinutes(date.getMinutes() + 13);
Momentjs Is by far the best library to use for any date calculation.
let endDate = moment().add({
d:646, //days
h: 5, //hours
m: 13
}).format('MMMM D, YYYY')
document.write(endDate)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.js"></script>
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);
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.