I want to restrict a user to only being able to add future dates in a HTML date input.
Instead of jQuery UI date picker I want to add HTML5 calender. Can anyone tell me how can I restrict the input to future dates?
You can use min and max attributes of HTML5 input date
HTML5 code
<input type="date" name="bday" min="2014-05-11" max="2014-05-20">
EDIT
You need to use jQuery to achieve it
jQuery code
$(function(){
var dtToday = new Date();
var month = dtToday.getMonth() + 1;
var day = dtToday.getDate();
var year = dtToday.getFullYear();
if(month < 10)
month = '0' + month.toString();
if(day < 10)
day = '0' + day.toString();
var maxDate = year + '-' + month + '-' + day;
$('#txtDate').attr('max', maxDate);
});
Explanation
max attribute of HTML5 input date takes month and day in double digit format.
Ex: 5 (Month) is not valid whereas 05 (Month) is valid
Ex: 1 (Day) is not valid whereas 01 (Day) is valid
So I have added below code
if(month < 10)
month = '0' + month.toString();
if(day < 10)
day = '0' + day.toString();
Check my updated fiddle
Refer fiddle demo
To build on #Chirag Vidani's answer, the date can be generated with fewer lines like this:
var now = new Date(),
// minimum date the user can choose, in this case now and in the future
minDate = now.toISOString().substring(0,10);
$('#my-date-input').prop('min', minDate);
Here is a PHP solution that gets today's date and sets it as the maximum.
<input type="date" name="bday" max="<?php echo date("Y-m-d"); ?>">
This will put it in the correct double-digit format for the day and month.
https://www.php.net/manual/en/function.date.php
Some others have asked the question about setting the max date to the current date. The suggested answers involve using JavaScript. But my solution was to use the server side language to generate the max parameter for the input. I know the OP didn't ask about a server-side approach. But this solution works well for me using C# Razor as my server language.
On the server I write:
#Html.TextBox("CurrentDate",
Model.CurrentDate.ToString("yyyy-MM-dd"),
new {
#class = "form-control",
#type = "date",
max = DateTime.Now.ToString("yyyy-MM-dd")
})
And then MVC outputs this Html:
<input class="form-control" id="CurrentDate" name="CurrentDate"
type="date" max="2016-11-10" value="2016-11-10">
With other server languages the approach would be to similarly generate the max parameter using the Server's date, which may or may not work if your requirement is to use the Client's date. For my situation the Server's date is what is needed because that's where the data is stored.
Use the max attribute which is the expected upper bound for the element's value.
<input type="date" max="2014-05-15"/>
Reference: http://www.w3.org/TR/html-markup/input.date.html
I have updated the Working fiddle
http://jsfiddle.net/9WVY8/16/
HTML & js:
var dateControler = {
currentDate : null
}
$(document).on( "change", "#txtDate",function( event, ui ) {
var now = new Date();
var selectedDate = new Date($(this).val());
if(selectedDate > now) {
$(this).val(dateControler.currentDate)
} else {
dateControler.currentDate = $(this).val();
}
});
<input type="date" value="<?php echo date("Y-m-d"); ?>" max="<?php echo date("Y-m-d"); ?>">
This worked for me.
Old Question But a solution, may help someone using JQuery:
$(document).ready(function () {
var today = new Date();
var day=today.getDate()>9?today.getDate():"0"+today.getDate(); // format should be "DD" not "D" e.g 09
var month=(today.getMonth()+1)>9?(today.getMonth()+1):"0"+(today.getMonth()+1);
var year=today.getFullYear();
$("#dpFromDate").attr('max', year + "-" + month + "-" + day);
});
The date format should be YYYY-MM-DD.
you can get that only using JS and HTML:
first let's get the actual date
let n = new Date();
let y = n.getFullYear();
let m = n.getMonth() + 1;
let d = n.getDate();
and lets do the minDate and Max date strings, and evaluate if we'll need put a 0 in the month/day to keep the required format:
if(m < 10)
m = '0' + m.toString();
else if(d < 10)
d = '0' + d.toString();
let minDate = y + '-' + m + '-' + d
let maxDate = y + '-' + "0"+(parseFloat(0+m) + 1) + '-' + d
after, lets set it in the HTML:
<input name="Fecha_end" type="date" id="#Fecha_end">
let Fecha_end_input = document.getElementById("#Fecha_end")
Fecha_end_input.setAttribute("min",minDate)
Fecha_end_input.setAttribute("max",maxDate)
Total:
HTML:
<input name="Fecha_end" type="date" id="#Fecha_end">
JS:
let Fecha_end_input = document.getElementById("#Fecha_end")
let n = new Date();
let y = n.getFullYear();
let m = n.getMonth() + 1;
let d = n.getDate();
if(m < 10)
m = '0' + m.toString();
else if(d < 10)
d = '0' + d.toString();
let minDate = y + '-' + m + '-' + d
let maxDate = y + '-' + "0"+(parseFloat(0+m) + 1) + '-' + d
Fecha_end_input.setAttribute("min",minDate)
Fecha_end_input.setAttribute("max",maxDate)
<script>
var today = new Date().toISOString().slice(0, 16);
document.getElementsByName("time_start")[0].max = today;
</script>
and input
id="time_start"
Related
I have used following code snippet to restrict previous dates in datetimelocal input. It works fine but it returns seconds and milliseconds too. How to get only hours and minutes?
$(document).ready(function(){
let elem = document.getElementById("application_start_date")
const iso = new Date().toISOString();
const minDate = iso.substring(0, iso.length - 1);
elem.value = minDate
elem.min = minDate
});
Just replace the substring parameters like this
If you want date and hous+minutes,
const mn = iso.substring(0,iso.length -5) //2022-02-17T05:34:06
If you want time only then,
const mn = iso.substring(11,iso.length -5) //05:34:06
Ok so I've been pretty much learning on my own and I ran into a problem that I cant seem to find a solution for.
The main goal is I have a google sheet with a start and end date that the user can change, the script pulls those dates and uses them in a MySQL query to pull the data between that date range.
Example: start date = 10/1/2021, end date = 10/22/21
Query: "select * from Table, where table.date >= start date AND table.dat <= end date"
See my code example below:
======================================================
var spreadsheet = SpreadsheetApp.getActive();
var sheet = spreadsheet.getSheetByName('DR_Campaign_Report');
var getStartDate = sheet.getRange(1,2).getValue();
var startDate = Utilities.formatDate(getStartDate,"GTM","MM/dd/yyyy");
var getEndDate = sheet.getRange(1,5).getValue();
var endDate = Utilities.formatDate(getEndDate,"GTM","MM/dd/yyyy");
var conn = Jdbc.getConnection(url, username, password);
var stmt = conn.createStatement();
var results = stmt.executeQuery
( 'SELECT m.Campaign as "Campaign",\n' +
'count(m.Campaign) as "Leads",\n' +
'count(m.Duplicate) as "Dups",\n' +
'count(m.Campaign) - count(m.Duplicate) as "Valid Leads",\n' +
'count(m.AppSet) as "Appts",\n' +
'SUM(IF(m.ZepID != "",1,0)) as "Transferred Appts"\n' +
'FROM intakeMani m\n' +
'WHERE date(m.IncomingDate) >= date('startDate') and date(m.IncomingDate) <= date('endDate')\n' +
'OR date(m.AppSet) >= date('startDate') and date(m.AppSet) <= date('endDate')\n' +
'GROUP BY m.Campaign with rollup'
);
The Error is here in the WHERE clause when its attempting to pull the google script variables startDate and endDate.
'WHERE date(m.IncomingDate) >= date('startDate') and date(m.IncomingDate) <= date('endDate')\n' +
'OR date(m.AppSet) >= date('startDate') and date(m.AppSet) <= date('endDate')\n' +
I attempted the double "'startDate'" and is still errors. see attached pic.
I fixed it, I had double " in the var getStartDate/GetEndDate fields and need to add " to the query "'+startdate+'"
Thank you for your help.
I am trying to set a min and max date within PrimeNG's . I would like for the "FROM DATE" input to not be older than 2 weeks from today's date. And the "TO DATE" input to not be more than 1 year away from today's date.
Here are my date fields.
<p-calendar [showIcon]="true" [minDate]="minDate" [readonlyInput]="true" placeholder="From Date" id="setter" ></p-calendar>
<p-calendar [showIcon]="true" [maxDate]="maxDate" [readonlyInput]="true"placeholder="To Date" id="setter"></p-calendar>
This is in my logic in the .ts file
ngOnInit() {
let today = new Date();
let month = today.getMonth();
let year = today.getFullYear();
let prevMonth = (month === 0) ? 11 : month -1;
let nextMonth = (month === 11) ? 0 : month + 4;
this.minDate = new Date();
this.maxDate = new Date();
this.minDate.setMonth(prevMonth);
this.maxDate.setMonth(nextMonth);
}
View of primeNG calender
From the requirements you stated, you would need one [maxDate] attribute on each date/time control. Have one variable that sets the max for From Date to nothing greater than two weeks from now:
this.fromDateMax = new Date(Date.now() + 12096e5);
And another variable to set the max date of To Date to one year from now:
this.toDateMax = new Date(new Date().setFullYear(new Date().getFullYear() + 1));
Both of these in the ngOnInit() will set the appropriate limits as you stated.
Here is stackblitz for reference and the two SO answers I used to get the date limits:
Javascript Date Plus 2 Weeks (14 days)
Add year to todays date
Only had to make a small chane to my nhOnit(). It looks like this now and it works exactly how I needed it to.
ngOnInit() {
let today = new Date();
let month = today.getMonth();
let year = today.getFullYear();
let nextMonth = (month === 11) ? 0 : month + 4;
this.minDate = new Date(Date.now() - 12096e5);
this.maxDate = new Date(new Date().setFullYear(new Date().getFullYear() + 1));
this.maxDate.setMonth(nextMonth);
}
I'm trying to combine date and time into one element in google app script array. This is for converting data from google sheet into google calendar.
I've 4 elements in my array; title, date, start time, end time. Each of them were retrieved by .getValues from google sheet.
title1 | Aug 08,2019 | 7:30 | 8:25
title2 | Aug 10,2019 | 8:30 | 9:25
I want to grab date and time from google sheet then createEvent in calendarApp.
//so with .getValues() in cArr variable from the table above I tried this code:
for (var i = 0; i <= cArr.length; i++){
CalendarApp.getCalendarById("myCalendarID").createEvent(cArr[i][0],cArr[i][2],cArr[i][2]);
};
The script were successfully run without error. But the event didn't appear in my calendar. I assume the events ever create in 1899 since it didn't specified the date in element [2] and [3].
Through some research, my best guess is to modify the array elements to be in 'MMM dd/yyyy, HH:mm' for both element [1] and [3]. But I just can't find a solution to do it. In the end, I want the result array like
[
["title1","Aug 08/2019, 7:30","Aug 08/2019, 8:25"],
["title2","Aug 10/2019, 8:30","Aug 10/2019, 9:25"]
]
Before I use this new array in .createEvent.
You can use the getDisplayValues() function [1] to obtain the string value of the cell, from this get the date info and create a Date object with that. Here is the code for that:
var cArr = sheet.getRange(13, 3, 2, 4).getDisplayValues();
for (var i = 0; i < cArr.length; i++){
var month = cArr[i][1].substring(0, 3);
var day = cArr[i][1].substring(4, 6);
var year = cArr[i][1].substring(7);
var startMinutes = cArr[i][2].substr(-2);
var startHours = cArr[i][2].substring(0, 2);
var endMinutes = cArr[i][3].substr(-2);
var endHours = cArr[i][3].substring(0, 2);
var startDate = new Date(month + " " + day + ", " + year + " " + startHours + ":" + startMinutes + ":00");
var endDate = new Date(month + " " + day + ", " + year + " " + endHours + ":" + endMinutes + ":00");
CalendarApp.getCalendarById("[mail]").createEvent(cArr[i][0], startDate, endDate);
};
[1] https://developers.google.com/apps-script/reference/spreadsheet/range#getdisplayvalues
Thank you so much for suggestions.
I found the work around solution for my problem. I will share it here for references.
The data retrieved from googlesheet were converted in to date object so the idea is to create a new string containing date, month, year, time(hour and minute) using concatenation then apply the string on new Date() function.
var day = Utilities.formatDate(cArr[1],"GMT+07:00","MMM dd");
var year = Utilities.formatDate(cArr[1],"GMT+07:00","yyyy");
var ST = Utilities.formatDate(cArr[2],"GMT+07:08","HH:mm");
var ET = Utilities.formatDate(cArr[3],"GMT+07:08","HH:mm");
//then I concatenate them together
var StartTime = new Date(day + " " + ST + " " + year);
var EndTime = new Date(day + " " + ET + " " + year);
I did this under .map() function on cArr for a better operation time then using a for loop to create event in CalendarApp.getCalendarById("myCalendarID").createEvent().
PS. I don't know why the time zone has to be GMT+7:08 but this is from my trials and errors to get this time-shift to work best for my project. Also I tried to make it correct to the second digit, but the object turned into 'Jan 1 1899 8:00:00' when I tried with GMT+7:07:48.
document.getElementById("appealTriggerDate").valueAsDate = vm.appealEntity.DateOfAppealTrigger();
I am using above line of code to display date in HTML5 date control. After I do this, I find that I am unable to edit date from the date control. The control does not set the date selected.
Fri Nov 15 2013 00:00:00 GMT-0500(Eastern Standard Time)
vm.appealEntity.DateOfAppealTrigger() returns date in above format. The date is as fetched from the database. I am not doing any formatting.
If I try to set the date in the format below, I can see the date set in the control, but edit it still not possible.
var dt= new Date(vm.appealEntity.DateOfAppealTrigger());
var month = dt.getMonth() + 1;
var day = dt.getDate();
if (month < 10)
month = "0" + month;
if (day < 10)
day = "0" + day;
document.getElementById("appealTriggerDate").value = dt.getFullYear() + "-" + month + "-" + day;