index.js
function resolveDate (base_url, input, callback) {

  var date = input;

  // If provided a date string
  if ((typeof input == "string" || typeof input == "number") && input !== "latest") {
    date = new Date(input);
  }

  // If provided a date object
  if (moment.isDate(date)) { return callback(null, date); }

  // If provided "latest"
  else if (input === "latest") {
    var latest = base_url + '/latest.json';
    request({
      method: 'GET',
      uri: latest,
      timeout: 30000
    }, function (err, res) {
      if (err) return callback(err);
      try { date = new Date(JSON.parse(res.body).date); }
      catch (e) { date = new Date(); }
      return callback(null, date);
    });
  }

  // Invalid string provided, return new Date
  else { return callback(null, new Date()); }
}
function resolveDate (base_url, input, callback) {

  var date = input;

  // If provided a date string
  if ((typeof input == "string" || typeof input == "number") && input !== "latest") {
    date = new Date(input);
  }

  // If provided a date object
  if (moment.isDate(date)) { return callback(null, date); }

  // If provided "latest"
  else if (input === "latest") {
    var latest = base_url + '/latest.json';
    request({
      method: 'GET',
      uri: latest,
      timeout: 30000
    }, function (err, res) {
      if (err) return callback(err);
      try { date = new Date(JSON.parse(res.body).date); }
      catch (e) { date = new Date(); }
      return callback(null, date);
    });
  }

  // Invalid string provided, return new Date
  else { return callback(null, new Date()); }
}
function resolveDate ( base_url , input , callback ) { var date = input ; // If provided a date string if ( ( typeof input == "string" || typeof input == "number" ) && input !== "latest" ) { date = new Date ( input ) ; } // If provided a date object if ( moment . isDate ( date ) ) { return callback ( null , date ) ; } // If provided "latest" else if ( input === "latest" ) { var latest = base_url + '/latest.json' ; request ( { method : 'GET' , uri : latest , timeout : 30000 } , function ( err , res ) { if ( err ) return callback ( err ) ; try { date = new Date ( JSON . parse ( res . body ) . date ) ; } catch ( e ) { date = new Date ( ) ; } return callback ( null , date ) ; } ) ; } // Invalid string provided, return new Date else { return callback ( null , new Date ( ) ) ; } }
Takes an input, either a date object, a date timestamp, or the string "latest"
and resolves to a native Date object.
@param  {String|Date}   input    The incoming date or the string "latest"
@param  {Function} callback The function to be called when date is resolved
Takes an input either a date object a date timestamp or the string latest and resolves to a native Date object .