UNPKG

react-native-surveys

Version:

Build your own forms, surveys and polls for your React Native apps.

159 lines (143 loc) 3.47 kB
function throwIfInvalidDate(date) { if (Object.prototype.toString.call(date, null) !== "[object Date]") { throw new Error("参数类型不对"); } } function daysInMonth(year, month) { return new Date(year, month + 1, 0).getDate(); } export function convertDate(date, format = "YYYY/MM/DD") { let str = format; const o = { "M+": date.getMonth() + 1, "D+": date.getDate(), "h+": date.getHours(), "m+": date.getMinutes(), "s+": date.getSeconds() }; if (/(Y+)/.test(format)) { str = str.replace( RegExp.$1, date .getFullYear() .toString() .substr(4 - RegExp.$1.length) ); } for (const k in o) { // eslint-disable-line if (new RegExp(`(${k})`).test(format)) { str = str.replace( RegExp.$1, RegExp.$1.length === 1 ? o[k] : `00${o[k]}`.substr(o[k].toString().length) ); } } return str; } export const getDayFormat = (date = new Date(), hasHours, timezone) => { const day = dayOfWeekAsString(date.getDay()); const number = getNumberWithOrdinal(date.getDate()); const month = monthAsString(date.getMonth()); const year = date.getFullYear(); let hours = ""; let zone = ""; if (hasHours) { hours = getHoursAndMinutes(date) + " "; zone = timezone; } return hours + day + ", " + month + " " + number + " " + year + " " + zone; }; function addZero(i) { if (i < 10) { i = "0" + i; } return i; } export function getHoursAndMinutes(d) { if (!d) d = new Date(); const h = addZero(d.getHours()); const m = addZero(d.getMinutes()); return h + ":" + m; } function dayOfWeekAsString(dayIndex) { return [ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" ][dayIndex]; } function monthAsString(monthIndex) { return [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ][monthIndex]; } function getNumberWithOrdinal(n) { const s = ["th", "st", "nd", "rd"], v = n % 100; return n + (s[(v - 20) % 10] || s[v] || s[0]); } export function returnTimeZone(date = "") { if (date) { if (date.split(",")[1]) return date.split(",")[1].substring(7); } return /\((.*)\)/.exec(new Date().toString())[1]; } export function nextYear(now, index = 0) { throwIfInvalidDate(now); return new Date( now.getFullYear() + index, now.getMonth(), now.getDate(), now.getHours(), now.getMinutes(), now.getSeconds() ); } export function nextMonth(now, index = 0) { throwIfInvalidDate(now); const year = now.getFullYear(); const month = now.getMonth() + index; const dayOfMonth = Math.min(now.getDate(), daysInMonth(year, month)); return new Date( year, month, dayOfMonth, now.getHours(), now.getMinutes(), now.getSeconds() ); } export function nextDate(now, index = 0) { throwIfInvalidDate(now); return new Date(now.getTime() + index * 24 * 60 * 60 * 1000); } export function nextHour(now, index = 0) { throwIfInvalidDate(now); return new Date(now.getTime() + index * 60 * 60 * 1000); } export function nextMinute(now, index = 0) { throwIfInvalidDate(now); return new Date(now.getTime() + index * 60 * 1000); } export function nextSecond(now, index = 0) { throwIfInvalidDate(now); return new Date(now.getTime() + index * 1000); }