-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyDate.js
142 lines (132 loc) · 3.56 KB
/
MyDate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import PropTypes from 'prop-types';
import React from 'react';
import { StyleSheet, Text } from 'react-native';
/*
Display Date/Time/DateTime. All are optional except
value. Note we just want a consistent date displayer
of the incoming date as it is, without any conversion.
value is expected to be UTC in yyyy-mm-ddTHH:MM:SSZ
We can do with parsing the date instead of separating.
*/
// TODO: update in line with what we will expect to come from
// real couchbase/roster
const MyComponent = ({
value,
incomingDateSeparator,
returnDate,
returnTime,
dateFormat,
separateDateWith,
timeFormat,
styles,
rest
}) => {
const txtStyle = styles !== undefined ? styles : defaultStyles.text;
const desiredOutputFormat =
dateFormat !== undefined ? dateFormat : 'yyyy-mm-dd';
const desiredTimeOutputFormat =
timeFormat !== undefined ? timeFormat : 'hh:mm';
const separateDateValuesWith =
separateDateWith !== undefined ? separateDateWith : '-';
const incomingValueSeparator =
incomingDateSeparator !== undefined ? incomingDateSeparator : '-';
// returnees
let date = '';
let time = '';
let displayedOutput = '';
try {
const parsedValue = value.split(' ');
if (returnDate) {
const datePart = parsedValue[0].split(incomingValueSeparator);
const year = datePart[0];
const month = datePart[1];
const day = datePart[2];
switch (desiredOutputFormat.toLowerCase()) {
case 'dd-mm-yyyy':
date =
day +
separateDateValuesWith +
month +
separateDateValuesWith +
year;
break;
case 'mm-dd-yyyy':
date =
month +
separateDateValuesWith +
day +
separateDateValuesWith +
year;
break;
default:
date =
year +
separateDateValuesWith +
month +
separateDateValuesWith +
day;
break;
}
displayedOutput = date;
}
if (returnTime) {
const timePart = parsedValue[1].split(':');
const hr = timePart[0];
const mm = timePart[1];
let ss = timePart[2]; // includes milisecond
const sec = ss.split('.');
// eslint-disable-next-line prefer-destructuring
ss = sec[0];
if (desiredTimeOutputFormat === 'hh:mm:ss') {
time = `${hr}:${mm}:${ss}`;
} else {
time = `${hr}:${mm}`;
}
}
if (returnDate && returnTime) {
displayedOutput = `${date} ${time}`;
} else if (returnDate && !returnTime) {
displayedOutput = date;
} else {
displayedOutput = time;
}
} catch (error) {
displayedOutput = value;
}
displayedOutput = displayedOutput.trim();
return (
<>
<Text style={txtStyle} {...rest}>{displayedOutput}</Text>
</>
);
};
const defaultStyles = StyleSheet.create({
text: {
color: '#000',
marginBottom: 4,
marginRight: 20
}
});
MyComponent.propTypes = {
value: PropTypes.string.isRequired,
incomingDateSeparator: PropTypes.string,
timeFormat: PropTypes.string,
styles: PropTypes.instanceOf(Object),
returnDate: PropTypes.bool,
returnTime: PropTypes.bool,
dateFormat: PropTypes.string,
separateDateWith: PropTypes.string,
rest : PropTypes.instanceOf(Object)
};
MyComponent.defaultProps = {
styles: undefined,
timeFormat: undefined,
returnDate: true,
returnTime: false,
incomingDateSeparator: undefined,
dateFormat: undefined,
separateDateWith: undefined,
rest : {}
};
const MyDate = React.memo(MyComponent);
export default MyDate;