Skip to content

Instantly share code, notes, and snippets.

@uqmessias
Created March 9, 2018 20:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uqmessias/db56f2a45bbce131ded455622359926b to your computer and use it in GitHub Desktop.
Save uqmessias/db56f2a45bbce131ded455622359926b to your computer and use it in GitHub Desktop.
// @flow
export type TimePeriod = 'am' | 'pm';
export type TimeOfTheDay = { hour: number, minute: number, period: TimePeriod };
const getTimeOfTheDay = (date: Date): TimeOfTheDay => ({
hour: date.getHours() > 12 ? date.getHours() - 12 : date.getHours(),
minute: date.getMinutes(),
period: date.getHours() >= 12 ? 'pm' : 'am',
});
const dateToPeriod = (startTime: Date, endTime: Date): string => {
const time = [];
const start = getTimeOfTheDay(startTime);
const end = getTimeOfTheDay(endTime);
time.push(`${start.hour}:${start.minute}`);
time.push(`${end.hour}:${end.minute}${end.period}`);
return time.join('-');
};
export default dateToPeriod;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment