How to set 15 min after system date using jQuery - html

How to set 15 min after time from the current time set to time input field using jQuery.
<input name="user_time" id="user_time" type="time" class="form-control" >

I have found this solution that works. I don't know what event you want to use, but for example I have use an onclic even on your input.
$('#user_time').click(function(){
var d = new Date();
var min = d.getMinutes() + 15
var date = d.getHours()+":"+ min;
$('#user_time').val(date);
});
Change the event and use the one you want.

You could:
create a new Date object: let d = new Date();
add 15 minutes: d.setMinutes(d.getMinutes() + 15);
define the new value: let user_time = d.getHours() + ':' + d.getMinutes();
set the new value with jQuery: $('#user_time').val(user_time); (or you can simply do it with Vanilla JS: document.getElementById('user_time').value = user_time;)
let d = new Date();
d.setMinutes(d.getMinutes() + 15);
let user_time = d.getHours() + ':' + d.getMinutes();
$('#user_time').val(user_time); // document.getElementById('user_time').value = user_time;
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input name="user_time" id="user_time" type="time" class="form-control" >

Related

How would I check for a proper custom input of mm/dd/yyyy in actionscript 3?

I have a DataField with editable="true" and format mm/dd/yyyy. Then lets say user typed in month mm section 13 which is not correct. How can I validate it as well as dd section and yyyy section and show a pop up when it's incorrect?
Here is what happening when apply button was clicked:
var newDate:Date = dfDate.selectedDate;
var month:String = (newDate.month + 1) < 10 ? "0" + (newDate.month + 1).toString() : (newDate.month + 1).toString();
var date:String = newDate.date < 10 ? "0" + newDate.date.toString() : newDate.date.toString();
var year:Number = newDate.getFullYear();
var dateString:String = month + "/" + date + "/" + year;
Button section:
<mx:FormItem id="itemDate">
<mx:DateField id="dfDate" yearNavigationEnabled="true" editable="true"/>
</mx:FormItem>
You can use just a simple regular expression
var customRegExp:RegExp = new RegExp(/(0[1-9]|1[012])[\/](0[1-9]|[12][0-9]|3[01])[\/](19|20)\d\d/);
And then use .test function along with some formatting stuff
var fmt:DateFormatter = new DateFormatter();
fmt.formatString = "MM/DD/YYYY";
if(!customRegExp.test(fmt.format(dfDate.selectedDate)))
{
Alert.show("Please input date in format MM/DD/YYYY");
enableForm(true);
}
Worked for me just fine and I think will work for others as well!
Here is code all together:
var customRegExp:RegExp = new RegExp(/(0[1-9]|1[012])[\/](0[1-9]|[12][0-9]|3[01])[\/](19|20)\d\d/);
var fmt:DateFormatter = new DateFormatter();
fmt.formatString = "MM/DD/YYYY";
if(!customRegExp.test(fmt.format(dfDate.selectedDate)))
{
Alert.show("Please input date in format MM/DD/YYYY");
enableForm(true);
}

Creating redirect link with calculated parameters

I am using the following code to redirect visitors on my website:
<meta charset="UTF-8">
<meta http-equiv="refresh" content="1;url=http://mylink.com">
<script type="text/javascript">
window.location.href = "http://mylink.com"
</script>
I would like to add dates to the link so that the link becomes this:
"http://mylink.com/startdate=2016-01-09;enddate=2016-02-09"
I would also like to set the startdate parameter set to tomorrow's date and enddate parameter to be calculated based on the startdate value.
Thanks in advance!
Although JavaScript is not tagged in your question, I would do something like this:
function formatDate(date) {
var days = date.getDate();
var months = date.getMonth() + 1;
var fixedDays = (days < 10)? ("0" + days) : days;
var fixedMonths = (months < 10)? ("0" + months) : months;
var result = (1900 + date.getYear()) + "-"
+ fixedMonths + "-" + fixedDays;
return result;
}
function createLink(baseUrl, daysExpiration) {
var tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);
var expire = tomorrow;
expire.setDate(expire.getDate() + daysExpiration);
var result = baseUrl +
"/startdate=" + formatDate(tomorrow) +
";enddate=" + formatDate(expire);
return result;
}
var link = createLink("www.ex.com", 30);
window.location = link;
Here's a JSFiddle.

Want to set 18 years back date when i click on date field in IOS device(IPhone6 &5)

I am facing issue in iOS device when I try to set the date back 18 years when I click on a date field. In fraction of seconds it gets changed to the current day. Here is my code. On Android this functionality is working fine.
HTML:
<input id="datePicker" type="date" />
JS:
$(document).ready( function() {
var now = new Date();
var day = ("0" + now.getDate()).slice(-2);
var month = ("0" + (now.getMonth() + 1)).slice(-2);
var yyyy = now.getFullYear()-18;
var day18yb = yyyy+"-"+(month)+"-"+(day) ;
$('#datePicker').val(''); // By default it should be empty so setting empty value
$('#datePicker').click(function(){ //When i click on date it should be 18 years back date
$('#datePicker').val(day18yb);
});
});

HTML - Time Input with up to 100 minutes

Is there any way to manipulate the html time input, so I can add up to 100 Minutes and 59 seconds?
No. Time input is for time. Your clock is more than 60 minutes?
Another way is to write custom input field with JavaScript. Or add number input field and then with JavaScript calculate hours/minutes/seconds ...
Edit: After re-reading the original post, I'm pretty sure I'm answering the wrong question here. But I'll leave it in case someone else finds it useful.
If you're referring to <input type="time">, you can access and modify the value like a normal input element:
var time = document.getElementById('time');
var button = document.getElementById('button');
function handleButtonClick() {
var parts = time.value.split(':');
var hour = parseInt(parts[0]);
var minute = parseInt(parts[1]);
hour += 1;
minute += 7;
// TODO: Need to validate the new time
time.value = hour + ':' + minute;
}
button.addEventListener('click', handleButtonClick, false);
<input id="time" type="time">
<button id="button">Process</button>
You can use Jquery for this:
function addMinutes(time, minsToAdd) {
function D(J){ return (J<10? '0':'') + J;};
var piece = time.split(':');
var mins = piece[0]*60 + +piece[1] + +minsToAdd;
return D(mins%(24*60)/60 | 0) + ':' + D(mins%60);
}
$("#add").click(function(){
var time = $("#time").val();
var new_time = addMinutes(time, '100');
alert(new_time);
});
Working DEMO

HTML5 input type time setting values not working

I have a form with input type time. I am trying to set values. But it is not showing. Here is my code.
<input type="time" name="exit" id="teacher-entry-exit-form-exit-input" class="form-control" value="08:56 AM" required="">
What am I doing wrong?
It's based on 24-hour time, therefore use value="08:56" for AM and value="20:56" for PM.
Example Here
<input type="time" value="08:56">
See: http://www.w3.org/TR/html-markup/input.time.html
Stephen, Giri,
all you have to do, in order to get AM/PM set, is to convert your 12-hour time string into 24-hour one, before passing it to the value attribute of input [type=time].
Here is a quick example how to convert 12-hour -> 24-hour time string.
const am_pm_to_hours = time => {
let hours = Number(time.match(/^(\d+)/)[1]);
let minutes = Number(time.match(/:(\d+)/)[1]);
const AMPM = time.match(/\s(.*)$/)[1];
if (AMPM.toLowerCase() === "pm" && hours < 12) hours = hours + 12;
if (AMPM.toLowerCase() === "am" && hours == 12) hours = hours - 12;
let sHours = hours.toString();
let sMinutes = minutes.toString();
if (hours < 10) sHours = "0" + sHours;
if (minutes < 10) sMinutes = "0" + sMinutes;
return `${sHours}:${sMinutes}`;
}
hope this helps :)
This worked for me :
<input class="form-control" type="time" name="time" value="<?=date("H:i", strtotime($dbObject['time'])); ?>" />
In this way, one can fetch the time column and display it like this using PHP and mysql.
Convert AM/PM Time string to 24 Hours Format. Example 9:30 PM to 21:30
function convertTimeFrom12To24(timeStr) {
var colon = timeStr.indexOf(':');
var hours = timeStr.substr(0, colon),
minutes = timeStr.substr(colon+1, 2),
meridian = timeStr.substr(colon+4, 2).toUpperCase();
var hoursInt = parseInt(hours, 10),
offset = meridian == 'PM' ? 12 : 0;
if (hoursInt === 12) {
hoursInt = offset;
} else {
hoursInt += offset;
}
return hoursInt + ":" + minutes;
}
console.log(convertTimeFrom12To24("12:00 AM"));
console.log(convertTimeFrom12To24("12:00 PM"));
console.log(convertTimeFrom12To24("11:00 AM"));
console.log(convertTimeFrom12To24("01:00 AM"));
console.log(convertTimeFrom12To24("01:00 PM"));