1

Seems like the following code should return a true, but it returns false.

How does this make sense? Is it right?

I tested it on Chrome and Firefox as well. Both are return false.

Test Code

console.log(file.lastModifiedDate == file.lastModifiedDate); //returns false

W3C File API Specification

interface File : Blob {
  readonly attribute DOMString name;
  readonly attribute Date lastModifiedDate;
};

Full Test Code

http://playground.html5rocks.com/#read_file_content_as_text

// Content section used alot

var content = document.getElementById('content');

if (!window.FileReader) {
  content.innerHTML = "<p>This browser doesnt support the File API</p>";
} else {
  // Page Layout
  content.innerHTML =
    '<p>Pick a text file or drag one into this area <br> <input type="file" id="file" /></p>' +
    '<b>Content:</b> <br><br> <pre id="file-content"></pre>' +
    '</p>';

  // Input handler
  document.getElementById('file').onchange = function() {
    readFileAsText(this.files[0]);
  };

  // Drag and drop methods
  content.ondragover = function(e) {
    e.preventDefault();
    return false;
  };
  content.ondrop = function(event) {
    e.stopPropagation();
    readFileAsText(event.dataTransfer.files[0]);
    return false;
  };

  function readFileAsText(file) {
    var reader = new FileReader();
    reader.readAsText(file);
    reader.onload = function(event) {
      document.getElementById('file-content').textContent = 
        event.target.result;
    };

    console.log(file.lastModifiedDate == file.lastModifiedDate); //returns false

    reader.onerror = function() {
      document.getElementById('file-content').innerHTML = 'Unable to read ' + file.fileName;
    };
  }
}​
rookiejava
  • 114
  • 4

2 Answers2

4

This happens if file.lastModifiedDate is NaN.

NaN != NaN is true in javascript

Edit due to new information

With the updated information, if it's checking for and returning a new date object every time, it won't be the same date. Objects are only equal in javascript if they're referencing the exact same object. Even if they have all the same properties, they won't be considered equal. If this is checking for the date each time it will be returning different objects, not the same one, and you'll get that result.

Javascript doesn't have a built in "deep-equals" , but you can look at this question for some guidance.

Community
  • 1
  • 1
Ben McCormick
  • 25,260
  • 12
  • 52
  • 71
  • I attached the full test code. In this case, file.lastModifiedDate has the right value (Date Object) not NaN. – rookiejava Mar 29 '13 at 17:17
4

From: https://www.w3.org/TR/2013/WD-FileAPI-20130912/#dfn-lastModifiedDate

On getting, if user agents can make this information available, this must return a new Date object initialized to the last modified date of the file.

I take this to mean that every time you get the lastModifiedDate it gets a new Date object.

The comparison is done on the object reference not on the values.

To compare the values you can do something like::

 console.log(file.lastModifiedDate.getTime() == file.lastModifiedDate.getTime()); 

EDIT: Note that in the current working spec, the attribute File.lastModifiedDate is replaced by File.lastModified which is comparable. As of March 18th 2016 is only supported in Chrome and Firefox.

mcdado
  • 718
  • 1
  • 8
  • 15
John Boker
  • 82,559
  • 17
  • 97
  • 130
  • Thanks. What I really want to know is what is the general rule (w3c file example is one of them) when I getting the (Object Type) attributes.Does it always make new Object to return? Is there golden rules? or depends on implementation? – rookiejava Mar 29 '13 at 20:02
  • I would say the general rule is to not compare reference types with ==. Compare their values. – John Boker Mar 30 '13 at 11:31
  • Date is mutable and can't be frozen. Returning a new Date instance each time you call lastModifiedDate lets the browsers avoid some complex issues. What happens if you modify the date? Would it be confusing if another piece of code read lastModifiedDate later, and saw your value? Would it be even more confusing if the Date would magically reset back to its old value? (also, implementing that in Chromium/V8 would be non-trivial) Because of these issues, WebIDL might move away from Date objects, and it is very likely that File will have a "lastModified" property that returns a number. – pwnall Dec 05 '13 at 02:14
  • I edited the answer, even if a couple of years old, because as @pwnall said the API is changing, and some browsers are already implementing it. – mcdado Mar 18 '16 at 16:40