最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to compare two dates from input? - Stack Overflow

programmeradmin0浏览0评论

there are two inputs

From:   <input id="from" type="date" placeholder="dd/mm/yy"><br>
To:   <input id="to" type="date" placeholder="dd/mm/yy">

I wanna pare these two inputs so as to ensure that the check out date is no earlier than the check in date. But how to pare? thx

there are two inputs

From:   <input id="from" type="date" placeholder="dd/mm/yy"><br>
To:   <input id="to" type="date" placeholder="dd/mm/yy">

I wanna pare these two inputs so as to ensure that the check out date is no earlier than the check in date. But how to pare? thx

Share Improve this question asked Nov 26, 2016 at 15:15 tweaktweaktweaktweak 711 silver badge6 bronze badges 2
  • 1 The html5 date input should and shall always be formatted like yyyy-MM-dd If you try to set a value that input value that has the format dd/mm/yy then you would get a invalid value. Just thinking that you should change your placeholder... – Endless Commented Nov 26, 2016 at 15:27
  • @Endless—not all implementations support date inputs. – RobG Commented Nov 26, 2016 at 21:09
Add a ment  | 

6 Answers 6

Reset to default 3

This is how I would do it

HTML

<script>
    function pareDates() {
        //Get the text in the elements
        var from = document.getElementById('from').textContent;
        var to = document.getElementById('to').textContent;

        //Generate an array where the first element is the year, second is month and third is day
        var splitFrom = from.split('/');
        var splitTo = to.split('/');

        //Create a date object from the arrays
        var fromDate = Date.parse(splitFrom[0], splitFrom[1] - 1, splitFrom[2]);
        var toDate = Date.parse(splitTo[0], splitTo[1] - 1, splitTo[2]);

        //Return the result of the parison
        return fromDate < toDate;
    }
</script>

<input id="from" type="date" placeholder="dd/mm/yy"><br>
<input id="to" type="date" placeholder="dd/mm/yy">
<button onclick="pareDates()">Compare</button>

Let me know how you get on.

For Browsers which support the "date" input, the input value is a DOMString representing a date in YYYY-MM-DD format, or empty. So (if the input values are not empty) you can just pare the two strings:

const FROM = document.getElementById("from").value;
const TO = document.getElementById("to").value;
return FROM < TO;

If you want your date format to be dd/mm/yy, you can use the following function to convert the string to date :

function pareDate(str){
 // str format should be dd/mm/yyyy. Separator can be anything e.g. / or -. It wont effect
 var dt   = parseInt(str.substring(0,2));
 var mon  = parseInt(str.substring(3,5));
 var yr   = parseInt(str.substring(6,10));
 var date = new Date(yr, mon-1, dt);
 return date;
}

Then simply get the values from inputs and pare them as date

var input1 = pareDate(document.getElementById('from').value);
var input2 = pareDate(document.getElementById('to').value);

The Date object will do what you want - construct one for each date, then pare them using the >, <, <= or >=.

You can get the value of your inputs by using javascript:

var fromValue = document.getElementById('from').value;
var toValue = document.getElementById('to').value;

then use the javascript date object to convert them to dates:

var fromDate = new Date(fromValue);
var toDate = new Date(toValue);

and remove time since midnight:

fromDate.setHours(0,0,0,0);
toDate.setHours(0,0,0,0);

then pare fromDate to toDate:

if (fromDate === toDate) { ... }

You can use diff method in Moment.js

var today = new Date();
var diff = moment().diff(today, 'days');
console.log(diff); // 0

More: http://momentjs./docs/#/displaying/difference/

You can use valueAsDate and then use the parator:

 input1 = document.getElementById('from');
 input2 = document.getElementById('to');

 date1 = input1.valueAsDate;
 date2 = input2.valueAsDate;
 console.log(date1 < date2 ? 'valid' : 'invalid');
发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>