Setting default value in html form date input - javascript

In a google-apps-script, I have an html form with two date inputs. I want to set the default values to be the prior week of the current day.
I cannot get the following scripts to work. Can anyone point out what I am doing wrong?
I am getting a "Object does not allow properties to be added or changed" error.
Code.gs:
function pickDates() {
var d = new Date();
var e = new Date();
d.setDate((d.getDate() - d.getDay() % 7) - 7);
e.setDate(e.getDate() - (e.getDay() + 1) % 7);
d = d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate();
e = e.getFullYear() + '-' + (e.getMonth() + 1) + '-' + e.getDate();
var htmlB = HtmlService.createHtmlOutputFromFile('Pick Dates').setWidth(200).setHeight(200);
htmlB.d = d;
htmlB.e = e;
SpreadsheetApp.getUi().showModalDialog(htmlB, 'Select Dates');
}
Pick Dates.html:
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<form>
Start Date<br>
<input id="start" type="date" name="startDay">
<br><br> End Date<br>
<input id="end" type="date" name="endDay">
<br><br>
<input type="button" value="Submit" onclick="google.script.run
.withSuccessHandler(google.script.host.close)
.sendEmails(this.parentNode)" />
</form>
<script>
document.getElementById('start').value = d;
document.getElementById('end').value = e;
</script>
</body>
</html>

To evaluate() the dates in the template, you will need to use createTemplateFromFile(). The error is because the HtmlOutput object returned from createHtmlOutputFromFile() can not have properties added like htmlB.d = d;. You can use a html template with scriptlets instead
function pickDates() {
var d = new Date();
var e = new Date();
d.setDate((d.getDate() - d.getDay() % 7) - 7);
e.setDate(e.getDate() - (e.getDay() + 1) % 7);
d = d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate();
e = e.getFullYear() + '-' + (e.getMonth() + 1) + '-' + e.getDate();
// create a template
var htmlB = HtmlService.createTemplateFromFile('Pick Dates');
htmlB.d = d;
htmlB.e = e;
// evaluate() returns HtmlOuput object
var modal_html = htmlB.evaluate().setWidth(200).setHeight(200);
SpreadsheetApp.getUi().showModalDialog(modal_html, 'Select Dates');
}
Pick Dates.html - add the printing scriptlets
...
<script>
document.getElementById('start').value = <?= d ?>;
document.getElementById('end').value = <?= e ?>;
</script>
</body>

Related

dates minimum and maximum, onchange event listener not working correctly for to date

I am trying to apply the minimum and maximun for two dates inputs, namely:
<input type="date" name="proposal_from" id="proposal_from" />
<input type="date" name="proposal_to" id="proposal_to" />
<script type="text/javascript">
var proposal_from = document.getElementById("proposal_from");
var proposal_to = document.getElementById("proposal_to");
function setFromDate() {
var today = new Date();
var dd = String(today.getDate()).padStart(2, '0');
var mm = String(today.getMonth() + 1).padStart(2, '0');
var minY = today.getFullYear();
var maxY = today.getFullYear() + 1;
var minimum = minY + '-' + mm + '-' + dd;
var maximum = maxY + '-' + mm + '-' + dd;
proposal_from.min = minimum;
proposal_from.max = maximum;
}
setFromDate();
proposal_from.addEventListener("change", function(){
var date = proposal_from.value.split("-");
var from_dd = date[2];
var from_mm = date[1];
var from_Y = date[0];
from_dd++;
var min_to_dd;
if(from_dd < 9){
min_to_dd = "0" + from_dd;
} else {
min_to_dd = from_dd;
}
var min_to_mm = from_mm;
var min_to_Y = from_Y;
from_Y++;
var max_to_dd;
if(from_dd < 9){
max_to_dd = "0" + from_dd;
} else {
max_to_dd = from_dd;
}
var max_to_mm = from_mm;
var max_to_Y = from_Y;
var minimum = min_to_Y + '-' + min_to_mm + '-' + min_to_dd;
var maximum = max_to_Y + '-' + max_to_mm + '-' + max_to_dd;
console.log(minimum);
console.log(maximum);
proposal_to.min = minimum;
proposal_to.max = maximum;
});
</script>
the event listener seems working now, the minimum and maximun dates get set as the date printed in the console should be and date values are completely correct. Before it was or example if I choose date 2021-10-04 I get minimum 2021-10-5 and 2022-10-5 instead of 2021-10-05 and 2022-10-05, is there anyway to do this with .padStart()?
You have to use the same logic that you used to pad zeros in setFromDate in your change event aswell.
Why this is needed?
You are performing incremental operation on from_dd using from_dd++ this will convert its type from String to Number. So convert it back to string again and perform you number padding logic.
function setFromDate() {
var today = new Date();
var dd = String(today.getDate()).padStart(2, '0');
var mm = String(today.getMonth() + 1).padStart(2, '0');
var minY = today.getFullYear();
var maxY = today.getFullYear() + 1;
var minimum = minY + '-' + mm + '-' + dd;
var maximum = maxY + '-' + mm + '-' + dd;
console.log(minimum)
proposal_from.min = minimum;
proposal_from.max = maximum;
}
setFromDate();
proposal_from.addEventListener("change", function () {
var date = proposal_from.value.split("-");
var from_dd = date[2];
var from_mm = date[1];
var from_Y = date[0];
from_dd++;
var min_to_dd = String(from_dd).padStart(2, '0')
var min_to_mm = from_mm;
var min_to_Y = from_Y;
from_Y++;
var max_to_dd = String(from_dd).padStart(2, '0')
var max_to_mm = from_mm;
var max_to_Y = from_Y;
var minimum = min_to_Y + '-' + min_to_mm + '-' + min_to_dd;
var maximum = max_to_Y + '-' + max_to_mm + '-' + max_to_dd;
proposal_to.min = minimum;
proposal_to.max = maximum;
});
<input type="date" name="proposal_from" id="proposal_from" />
<input type="date" name="proposal_to" id="proposal_to" />

Convert input into string/var and use it in an alert

I am using an input and want to covert it into a string/var to print it out in an alert after i click on a button.
I need the input of "Forename:" to add into the button "send" with "onlcick".
The alert is just a debuger. Instead of alert i want to use a function. Using onclick="printPDF(foreName+' '' ' + da + '.' + mo + '.' + ye + '_' + h + ':' + m + ':' + s + '.pdf');
function startTime() {
var today = new Date();
var da = today.getDate();
var mo = today.getMonth() + 1;
var ye = today.getFullYear();
var h = today.getHours();
var m = today.getMinutes();
var s = today.getSeconds();
m = checkTime(m);
s = checkTime(s);
}
function checkTime(i) {
if (i < 10) {
i = "0" + i
}; // add zero in front of numbers < 10
return i;
}
<p>
<label>Forename:</label>
<input class="w3-input w3-white" id="some1" type="text" name="some1" value="asd" maxLength="200">
</p>
<button id="snbtn1" type="submit" class="w3-btn w3-green" onclick="alert(' ' + da + '.' + mo + '.' + ye + '_' + h + ':' + m + ':' + s + '.pdf');">send</button>
<script>
function startTime() {
var today = new Date();
var da = today.getDate();
var mo = today.getMonth()+1;
var ye = today.getFullYear();
var h = today.getHours();
var m = today.getMinutes();
var s = today.getSeconds();
m = checkTime(m);
s = checkTime(s);
var foreName = document.getElementById('some1').value;
alert(foreName+' ' + da + '.' + mo + '.' + ye + '_' + h + ':' + m + ':' + s + '.pdf');
}
function checkTime(i) {
if (i < 10) {i = "0" + i}; // add zero in front of numbers < 10
return i;
}
</script>
<p>
<label>Forename:</label>
<input class="w3-input w3-white" id="some1" type="text" name="some1" value="asd" maxLength="200">
</p>
<button id="snbtn1" type="submit" class="w3-btn w3-green" onclick="startTime();">send</button>
You should put your onclick code into a function in your javascript code, by getting the button element and adding an event listener to it. In the same way you get the button in your javascript code you can get the input and it's value:
var today = new Date();
var da = today.getDate();
var mo = today.getMonth()+1;
var ye = today.getFullYear();
var h = today.getHours();
var m = today.getMinutes();
var s = today.getSeconds();
//m = checkTime(m);
//s = checkTime(s);
document.getElementById("snbtn1").addEventListener("click", function() { // Get the button and add an onclick event listener to it
alert(' ' + da + '.' + mo + '.' + ye + '_' + h + ':' + m + ':' + s + '.pdf');
alert(document.getElementById("some1").value); // Get the input by it's id and alert the value
});
<p>
<label>Forename:</label>
<input class="w3-input w3-white" id="some1" type="text" name="some1" value="asd" maxLength="200">
</p>
<button id="snbtn1" type="submit" class="w3-btn w3-green">send</button>
https://developer.mozilla.org/he/docs/Web/API/Document/getElementById
https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener

JavaScript is appending to a Div tag but with some strange behaviour

I have a script that for every click of the button takes the time, applies #gmail.com to the end and appends it to a defined div tag.
where the problem lies is that every time i hit the button, the first line changes to the current time, an additional line is appended each time with the correct details and they don't update it is just the first line.
can someone suggest a way to stop the first line from updating each time?
function Time() {
var d = new Date();
var dd = d.getDate();
var mm = d.getMonth() + 1; //January is 0!
var yy = d.getFullYear().toString().substr(2, 2);
var j = document.getElementById("demo").innerHTML = dd + "" + mm + "" + yy + "" + d.getHours() + "" + d.getMinutes() + "" + d.getSeconds() + "#gmail.com<br />";
$(document).ready(function() {
$("button").click(function() {
if (dd < 10) {
dd = '0' + dd
}
if (mm < 10) {
mm = '0' + mm
}
$("#demo").append(j);
});
});
return j;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<h2>Click to generate time</h2>
<button onclick="Time()">Generate</button>
<div id="demo"></div>
What I expect to happen each time i press the button is for it to append the following:
2591695115#gmail.com
2591695116#gmail.com
2591695117#gmail.com
2591695118#gmail.com
...etc
You are calling Time() onclick in your HTML. You do not need to say $("button").click again in your jquery.
Also, you do not need document.getElementById("demo").innerHTML when assiging the string to var j.
See the following code:
function Time() {
var d = new Date();
var dd = d.getDate();
var mm = d.getMonth() + 1; //January is 0!
var yy = d.getFullYear().toString().substr(2, 2);
var j = dd + "" + mm + "" + yy + "" + d.getHours() + "" + d.getMinutes() + "" + d.getSeconds() + "#gmail.com<br />";
if (dd < 10) {
dd = '0' + dd
}
if (mm < 10) {
mm = '0' + mm
}
$("#demo").append(j);
return j;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<h2>Click to generate time</h2>
<button onclick="Time()">Generate</button>
<div id="demo"></div>
You just need to append the result from Time function.
$(document).ready(function () {
$("button").click(function () {
$("#demo").append(Time());
});
});
function Time() {
var d = new Date();
var dd = d.getDate();
var mm = d.getMonth() + 1; //January is 0!
var yy = d.getFullYear().toString().substr(2, 2);
var j = dd + "" + mm + "" + yy + "" + d.getHours() + "" + d.getMinutes() + "" + d.getSeconds() + "#gmail.com<br />";
return j;
}
This should work. The problem was the structure of your function. I took the button click outside of the Time() function and also changed var j to only declare the time instead of also already adding it to the HTML.
Also, by declaring var j after your if statements the date now displays correctly.
function Time() {
var d = new Date();
var dd = d.getDate();
var mm = d.getMonth() + 1; //January is 0!
var yy = d.getFullYear().toString().substr(2, 2);
if (dd < 10) {
dd = '0' + dd
}
if (mm < 10) {
mm = '0' + mm
}
var j = dd + "" + mm + "" + yy + "" + d.getHours() + "" + d.getMinutes() + "" + d.getSeconds() + "#gmail.com<br />";
$("#demo").append(j);
}
$("button").click(function() {
Time();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<h2>Click to generate time</h2>
<button>Generate</button>
<div id="demo"></div>

select date from datepicker after i want next year date in next field

from date selected by the datepicker in dd-mm--yy format after this i need next year date with -1 day..i.e From:29-07-2016 then To:28-07-2016 like this...please guyz help me..i m sharing my source code
$('#oldDate').on('change', function(e){
var oldDate = new Date(this.value);
$('.datepicker').datepicker({dateFormat:'dd-mm-yy'});
$('#old').html(new Date(oldDate));
oldDate.setDate(oldDate.getDate()-1);
oldDate.setFullYear(oldDate.getFullYear()+1);
var day = ("0" + oldDate.getDate()).slice(-2);
var month = ("0" + (oldDate.getMonth() + 1)).slice(-2);
var today = oldDate.getFullYear()+"-"+(month)+"-"+(day);
// var today = oldDate.(day)+"-"+(month)+"-"+getFullYear();
// alert(today);
$('#new').val(today);
<p class="left">
<label for="from">FROM:</label>
<input type="text" name="from" id="oldDate" class="datepicker"/>
</p>
<p class="pull-right">
<label for="to">TO:</label>
<input type="text" name="to" id="new">
</p>
To achieve expected result, use datepicker and then use on change function to make work
JS:
$('.datepicker').datepicker({
dateFormat: 'dd-mm-yy'
}).datepicker("setDate", "0");
$('#oldDate').click(function() {
$('#oldDate').datepicker('setDate', new Date());
});
$('#oldDate').on('change', function(e) {
console.log(this.value)
var x = this.value;
var from = x.split("-");
var f = new Date(from[2], from[1] - 1, from[0]);
var oldDate = new Date(f);
$('#old').html(new Date(oldDate));
oldDate.setDate(oldDate.getDate() - 1);
oldDate.setFullYear(oldDate.getFullYear() + 1);
console.log(oldDate)
var day = ("0" + oldDate.getDate()).slice(-2);
var month = ("0" + (oldDate.getMonth() + 1)).slice(-2);
var today = (day) + "-" + (month) + "-" + oldDate.getFullYear();
// var today = oldDate.(day)+"-"+(month)+"-"+getFullYear();
// alert(today);
$('#new').val(today);
});
Codepen-http://codepen.io/nagasai/pen/XKBZmN
Check this :
<script type="text/javascript">
var dt = new Date('2016/12/10');
var month = dt.getMonth()+1;
var day = dt.getDate()-1;
var year = dt.getFullYear()+1;
alert(month + '-' + day + '-' + year);
</script>

displaying current date in a text box - javascript

I am having great difficulty getting the current date to be inputted into a text box within a form i am creating as a default value. at the moment i have the following code, which i believe creates the current date, followed by the text box code which i am unsure on how to modify in order for the date to be displayed inside.
function getDate(){
var todaydate = new Date();
var day = todaydate.getDate();
var month = todaydate.getMonth() + 1;
var year = todaydate.getFullYear();
var datestring = day + "/" + month + "/" + year;
document.getElementById("frmDate").value = datestring();
}
<input type="text" name="frmDateReg" required id="frmDate" value="getDate()">
if anyone could suggest how to create todays date and input it into the textbox as default it would be greatly appreciated. (Please excuse any format issues as i am new to stack overflow) Thanks
You've got the right idea. It's just out of order:
<input type="text" name="frmDateReg" required id="frmDate" value="">
function getDate(){
var todaydate = new Date();
var day = todaydate.getDate();
var month = todaydate.getMonth() + 1;
var year = todaydate.getFullYear();
var datestring = day + "/" + month + "/" + year;
document.getElementById("frmDate").value = datestring;
}
getDate();
Your code is correct, except that adding the function call in the value doesn't do anything. You need something else to trigger the function. The way I have it there, it will execute automatically when the page loads.
Aslo, datestring is not a function. It's just a variable. So you can leave off the ()
Try this Jquery code, this will work
$(document).ready(function () {
var dateNewFormat, onlyDate, today = new Date();
dateNewFormat = today.getFullYear() + '-';
if (today.getMonth().length == 2) {
dateNewFormat += (today.getMonth() + 1);
}
else {
dateNewFormat += '0' + (today.getMonth() + 1);
}
onlyDate = today.getDate();
if (onlyDate.toString().length == 2) {
dateNewFormat += "-" + onlyDate;
}
else {
dateNewFormat += '-0' + onlyDate;
}
$('#mydate').val(dateNewFormat);
});
razor view for this
#Html.TextBoxFor(m => m.StartedDate, new { #id = "mydate", #type = "date", #class = "form-control" })
This simple solution worked out for me. You can show the current date using window.onload function of javascript. See the output.
var todaydate = new Date();
var day = todaydate.getDate();
var month = todaydate.getMonth() + 1;
var year = todaydate.getFullYear();
var datestring = day + "/" + month + "/" + year;
window.onload = function(){
document.getElementById("date").value = datestring;
}
<input type="text" id="date"/ >
<html>
<body onload="myFunction()">
Date: <input type="text" id="demo"/>
<script>
function myFunction() {
document.getElementById('demo').value= Date();
}
</script>
</body>
</html>
Use this code. this will helpful
<input type="text" name="frmDateReg" required id="frmDate" >
<script>
function getDate(){
var todaydate = new Date();
var day = todaydate.getDate();
var month = todaydate.getMonth() + 1;
var year = todaydate.getFullYear();
var datestring = day + "/" + month + "/" + year;
document.getElementById("frmDate").value = datestring; //don't need ()
}
document.getElementById("frmDate").onload = getDate();
</script>
html
<input type="text" id="frmDate"></input>
JavaScript
var date = new Date();
document.getElementById("frmDate").value = (date.getMonth() + 1) + '/' + date.getDate() + '/' + date.getFullYear();
Jsfiddle Demo
<script TYPE="text/javascript" LANGUAGE=JAVASCRIPT>
var currentDate = new Date();
var date1 = currentDate.getMonth()+1;
var mon = currentDate.getDate();
if (mon<10)
mon="0"+mon
var year = currentDate.getYear();
var hour = currentDate.getHours();
var dn="pm"
if (hour<12)
dn="am"
if (hour>12)
hour=hour-12
if (hour==0)
hour=12
var minute = currentDate.getMinutes();
if (minute < 10){
minute = "0" + minute
}
var second = currentDate.getSeconds();
if (second < 10){
second = "0" + second
}
var today = date1+"/"+mon+"/"+year+" Time: "+hour+":"+minute+":"+second+" "+dn
var filePath = "\\\\servername\\maindirectory\\somedirectory\\filenametopostto.xlsx";
function setDate()
{
f1.tDate.value=today;
}
</script>
Result in your text box is:
5/28/2019 Time: 3:03:01 pm
If you want to show the current date on the input field date
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth() + 1; //January is 0!
var yyyy = today.getFullYear();
if (dd < 10) {
dd = '0' + dd
}
if (mm < 10) {
mm = '0' + mm
}
// today = yyyy + '/' + mm + '/' + dd;
today = `${yyyy}-${mm}-${dd}`;
document.getElementById('dateto1').value = today;
<div class="col-lg-4">
<label>Date</label>
<input type="date" name="dateto1" id="dateto1" class="form-control">
</div>

Categories