1

I try search by date from directory tree. But date is in file creation date is in filename and I want search between startDate and endDate.

enter image description here

My HTML:

<div class="row">
<div style="float:left; width:17%;">
    <input type="text" style="height:37px;" id="startDate" class="form-control" placeholder="DD/MM/YY">
</div> 
<div style="float:left; width:17%; margin:0 1%;">
    <div class="input-group">
    <input type="text" style="width:97.71px;" id="endDate" height:37px;" class="form-control" placeholder="DD/MM/YY" />
    <span class="input-group-btn">
        <button style="padding: 7.5px 12px; left:-1px;" class="btn btn-default" id="DateSearch" type="button"><i class="fa fa-search"></i></button>
    </span>
    </div>
</div>

Jquery: Didn't figure out how to do it. I found some datePickers but I dont want use them and found dataTables. I want search in my directory tree by file creation date what is in filename.

Free NAS
  • 15
  • 8
  • You will have to get the file names first and then search --- http://stackoverflow.com/questions/6994212/is-there-a-way-to-return-a-list-of-all-the-image-file-names-from-a-folder-using – Tasos Sep 04 '16 at 14:51
  • `$.get(''+ajaxcall, function(data) { $('#dir-links').html(data); $('#dir-links-backup').html(data); bindTreeLinks(post_action); });` with this im getting all filenames to directory tree. My default search working well but I dont know how to make date search.. @Tasos – Free NAS Sep 04 '16 at 14:55
  • Get the filenames, iterate one by one and split with (___) then with (#) and then (-). That should give you the date of the file name. eg (18) (02) (2016) From then on you need to campare with your chosen dates. I suggest to use the example in the above link to get the filesnames and iterate through them. Then check here on how to split -- http://www.w3schools.com/jsref/jsref_split.asp -- http://stackoverflow.com/questions/16711504/how-to-split-the-string-using-jquery-or-javascript – Tasos Sep 04 '16 at 15:22
  • I think splitting isn't hardest work what to do. I want know how I can search between two dates or something. – Free NAS Sep 04 '16 at 15:27
  • You do that by comparing -- I do a demo for you – Tasos Sep 04 '16 at 15:43
  • Okay :) This will be nice of you :) @Tasos – Free NAS Sep 04 '16 at 16:41
  • There you go, i simplified it using (new date) to get the mili seconds and compare -- https://jsfiddle.net/3x9ozpbL/ – Tasos Sep 04 '16 at 17:34

1 Answers1

1

Split as i mentioned in my Comments to make year-month-date in this format and compare each file date with your from - to range

example

Html

<p class="date">2013-08-4</p>
<p class="date">2014-09-5</p>
<p class="date">2015-09-5</p>
<p class="date">2016-09-5</p>

Code

    $(function() {
  var from = new Date("2013-09-4").getTime();
  var to = new Date("2014-09-8").getTime();

  $(".date").each(function(index, value) {
    var dates = $(this).text();

    if (from <= new Date(dates).getTime() && to >= new Date(dates).getTime()) {

      $(this).css("color", "blue");

    }

  });

});

Output

enter image description here

Demo

You could do the same in PHP using strtotime($the_date); which gives you minutes

Tasos
  • 5,321
  • 1
  • 15
  • 26
  • You should probably stick the to and from `Date(from).getTime()` in vars outside the loop, so you don't work it out every time. (Not important, but would be quicker and might make that large IF easier to read) – DBS Sep 04 '16 at 17:51
  • @DBS -- yeah that's a point -- i know it, but forgot it :)) cheers – Tasos Sep 04 '16 at 17:54