restrict previous dates in datetimelocal input - html

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

Related

Google Script MYSQL QUERY with Variables from google sheets

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.

Date validation logic within PrimeNg's <pcalender>

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);
}

Inserting data using SET and NOW() with node.js to mySQL Db

Currently this is my code, which inserts the new_item correctly
var new_item = {id_user: id, id_restaurant: id_rest, type: 3, date: date};
connection.query("INSERT INTO table SET ?", [new_item], function(err, results) {
if(err){
...
}
...
}
But now I don't want to send the date with it, I want to use SET ? and do something likedate = NOW() basically I want to insert this:
var new_item = {id_user: id, id_restaurant: id_rest, type: 3};
and use date = NOW()-INTERVAL 6 hour to assign the date.
EDIT: I posted I only wanted Now() but actually I want to set Interval it before inserting.
I think it is better to use moment.js.
You can use Date.now which returns current timestamp(milliseconds), so make sure to format it. The code is as follows:
var a = moment(Date.now()).format('YYYY-MM-DD HH:mm:ss');
Please let me know if this works out
new Date().toISOString().slice(0,10) + " " + new Date().toISOString().slice(11, 19)
same as 'YYYY-MM-DD HH:mm:ss'
Using NOW() is executed by the database interpreter and sets the database time.
You could create a new variable to use in your SQL statement and set the wanted content to that one.
var new_item1 = {id_user: id, id_restaurant: id_rest, type: 3, date: date};
var new_item2 = {id_user: new_item1.id_user, ...};
connection.query("INSERT INTO table SET ?", [new_item2], function(err, results) {
if(err){
...
}
...
}
Then you could configure your column to set the NOW() timestamp as default.

Restrict future dates in HTML5 date input

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"

calculate the time passed after an entry of date with the current time of the day

PreparedStatement pStmt = con.prepareStatement("insert into mydate values(?,?)");
java.util.Date now = new java.util.Date();
pStmt.setDate( 1, new java.sql.Date( now.getTime()) );
pStmt.setTimestamp( 2, new java.sql.Timestamp( now.getTime() ) );
pStmt.executeUpdate();
QUERY1:
I am creating post a comment module .I want to display the time when user posted their comments.also i want to show the time passed after they have posted a comment.
like 2 days ago ,5 min before etc.
QUERY2:
i also want to change the format of the date to dd/mm/yyyy.
For displaying the date formatted you can use java.text.SimpleDateFormat or
org.apache.commons.lang.time.FastDateFormat
For the time intervals you can use org.apache.commons.lang.time.DurationFormatUtils
Here is a sample:
//this will get the date in format dd/mm/yyyy
FastDateFormat fmt = FastDateFormat.getInstance("dd/MM/yyyy");
String txt = fmt.format(date);
//this will get the interval
long ival = System.currentTimeMilis()-date.getTime();
String interval = DurationFormatUtils.formatDurationWords(ival, true, true);