I'm asking the user to type a meeting description in one field. I'd like to intelligently guess/parse when a user includes a time in this input, to save them from having to fill out a separate time input field.
Google Calendar uses this kind of parsing to pre-fill time fields.
Example inputs:
I'd be grateful for any help!
I know you are asking for a JS solution, but I wrote some code in PHP that you might want to check and see if the logic helps you with your project.
<?php
$string1 = "Meet at Burger King at 2:30";
$string2 = "Denny's 2pm";
$string3 = "I want to buy 2 phones at 1";
print iterateString($string1). "- It should return 2:30<br>";
print iterateString($string2). "- It should return 2pm<br>";
print iterateString($string3). "- It should return 1:00<br>";
function isTime($date, $formats=array("H:i","G:i","Ha","ha", "Ga", "ga")) {
foreach($formats as $format) {
$d = DateTime::createFromFormat($format, $date);
$fd = $d ? $d->format($format) : false;
if ($d && $fd == $date) {
return $fd;
}
}
return false;
}
function iterateString($string) {
$singleTime = false;
$words = explode(" ",$string);
foreach($words as $word) {
if ($singleTime && ctype_digit($word)) {
$word = $word . ":00";
}
if($time = isTime($word)) {
return $time;
}
if($word == 'at') {
$singleTime = true;
} else {
$singleTime = false;
}
}
return "error";
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.