diff --git a/diskuy/src/ThreadList.js b/diskuy/src/ThreadList.js index 0b7590314357f1e66b4bcd72b02bd96d1577b8fe..cc72e90900d4ce955f83b19da398945441b12299 100644 --- a/diskuy/src/ThreadList.js +++ b/diskuy/src/ThreadList.js @@ -1,6 +1,9 @@ import './ThreadList.css'; +import { tConvert, translate } from './helpers/time-util' + export default function ThreadList(props){ + const time = translate(props.time) return ( <div id="threadCard"> <div id="threadCardHeader"> @@ -8,7 +11,7 @@ export default function ThreadList(props){ </div> <div id="threadCardContent"> <p id="topic">{props.topic}</p> - <p id="creator">By {props.user} - {props.time} - {props.points} </p> + <p id="creator">By {props.user} - {time} - {props.points} </p> </div> </div> ) diff --git a/diskuy/src/Threads/Post.js b/diskuy/src/Threads/Post.js index b0db2751a767148bed87286f4075efea38430bb8..dfda02bdb0fa67871d8a4b0d3c5202ebce6a7ff5 100644 --- a/diskuy/src/Threads/Post.js +++ b/diskuy/src/Threads/Post.js @@ -5,6 +5,7 @@ import AuthService from '../services/auth.service' import authHeader from '../services/auth-header' import React, { useState, useEffect} from 'react' import { loggedIn } from '../services/loggedInService' +import { translate } from '../helpers/time-util'; const LINK = 'http://localhost:4000/api'; export default function Post(props){ @@ -50,12 +51,14 @@ export default function Post(props){ } } + const time = translate(props.time) + return ( <div id="post"> <div id="postHeader"> <div id="headerData"> <div id="round"></div> - <p id="creator">By {props.user} - {props.time} </p> + <p id="creator">By {props.user} - {time} </p> </div> { AuthService.getCurrentUserId() === props.user_id && ( diff --git a/diskuy/src/helpers/time-util.js b/diskuy/src/helpers/time-util.js new file mode 100644 index 0000000000000000000000000000000000000000..202b391012f5129c13efbc19c80164421cfa8465 --- /dev/null +++ b/diskuy/src/helpers/time-util.js @@ -0,0 +1,25 @@ +const monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September","October", "November", "December"]; + +function tConvert (time) { + // Check correct time format and split into components + time = time.toString ().match (/^([01]\d|2[0-3])(:)([0-5]\d)(:[0-5]\d)?$/) || [time]; + + if (time.length > 1) { // If time format correct + console.log(time) + time = time.slice (1, 4); // Remove full string match value + time[5] = +time[0] < 12 ? ' AM' : ' PM'; // Set AM/PM + time[0] = +time[0] % 12 || 12; // Adjust hours + } + return time.join (''); // return adjusted time or original string +} + +function translate(time) { + const timeSplit = time.split('T') + const date = new Date(timeSplit[0]) + + const fullDate = `${date.getDay()} ${monthNames[date.getMonth()]} ${date.getFullYear()}` + + return `${fullDate}, ${tConvert(timeSplit[1])}` +} + +export {tConvert, translate}