代码按照您的要求工作,假设您以某种方式将日期和时间代码混合在一起…因此时间码也有8个字符。
我想看看您是如何创建这个字符串的,您应该能够在代码的前面将日期和时间值彼此分离,并避免这种讨厌的黑客攻击。
演示
$(document).on("change keyup paste click", "#dateTime", function() {
var dateTime = $(this).val();
// Split on '-'
var el = dateTime.split('-');
// Remove the time - we know rimes will always be entered as 'xx:xx:xx' - i.e. 8 characters
el[2] = el[2].substring(0, el[2].length - 8);
// Print to console
$("#dateOutput").text( el );
});
$("#dateTime").click();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p>You can test out the function below</p>
<p>Remember that it expects an eight character time string at the end of the string</p>
<input id="dateTime" value="2018-12-2417:25:33">
<p id="dateOutput"></p>