{"size": 1581, "ext": "js", "max_stars_repo_path": "src/services/dateConversions.js", "max_stars_repo_name": "thinkful-ei-heron/Capstone-Group3-Client", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/services/dateConversions.js", "max_issues_repo_name": "thinkful-ei-heron/Capstone-Group3-Client", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/services/dateConversions.js", "max_forks_repo_name": "thinkful-ei-heron/Capstone-Group3-Client", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 28.2321428571, "max_line_length": 66, "alphanum_fraction": 0.6198608476}
import * as firebase from 'firebase/app';
import React from 'react';

const dateConversions = {
  //Convert <input type='date'> format to firestore timestamp
  dateToTimestamp(date) {
    date.setMinutes(date.getMinutes() + date.getTimezoneOffset());
    return firebase.firestore.Timestamp.fromDate(new Date(date));
  },

  //Convert firestore timestamp to <input type='date'> format
  TStoFormDate(ts) {
    const date = ts.toDate();
    date.setMinutes(date.getMinutes() - date.getTimezoneOffset());

    return date.toJSON().slice(0, 10);
  },

  //Convert firestore timestamp to display format
  TStoDisplayDate(ts) {
    const date = ts.toDate();
    date.setMinutes(date.getMinutes() - date.getTimezoneOffset());
    const year = date.getFullYear();
    const month = ('0' + (date.getMonth() + 1)).slice(-2);
    const day = date.toJSON().slice(8, 10);

    return `${month}/${day}/${year}`;
  },

  //get the number of days overdue
  dateDiff(deadline) {
    let currDate = new Date();
    let dueDate = new Date(deadline.seconds * 1000);

    if (currDate > dueDate) {
      let res = Math.abs(currDate - dueDate) / 1000;
      let days = Math.floor(res / 86400);
      return (
        <span className="ProjectBar__overdue">
          Overdue by {days + 1} day(s)
        </span>
      );
    } else if (currDate < dueDate) {
      let res = Math.abs(currDate - dueDate) / 1000;
      let days = Math.floor(res / 86400);
      if (days + 1 === 0) {
        return <h2>Due Today</h2>;
      }
    } else {
      return null;
    }
  },
};

export default dateConversions;
