How to use the date-fns.isValid function in date-fns

To help you get started, we’ve selected a few date-fns examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github hashicorp / vault / ui / app / helpers / parse-date-string.js View on Github external
export function parseDateString(date, separator = '-') {
  // Expects format MM-yyyy by default: no dates
  let datePieces = date.split(separator);
  if (datePieces.length === 2) {
    if (datePieces[0] < 1 || datePieces[0] > 12) {
      throw new Error('Not a valid month value');
    }
    let firstOfMonth = new Date(datePieces[1], datePieces[0] - 1, 1);
    if (isValid(firstOfMonth)) {
      return firstOfMonth;
    }
  }
  // what to return if not valid?
  throw new Error(`Please use format MM${separator}yyyy`);
}
github bakdata / conquery / frontend / lib / js / common / helpers / dateHelper.js View on Github external
function handleRaw(what, value, displayDateFormat) {
  const denseFormat = displayDateFormat.replace(/[-\/\.]/g, "");
  // Assuming the format consists of 2 M, 2 d, and 2 y

  const dIdx = denseFormat.indexOf("d");
  const d = value.substring(dIdx, dIdx + 2);

  const mIdx = denseFormat.indexOf("M");
  const m = value.substring(mIdx, mIdx + 2);

  const yIdx = denseFormat.indexOf("y");
  const y = value.substring(yIdx, yIdx + 4);

  let date = new Date(parseInt(y), parseInt(m) - 1, parseInt(d));

  date = isValid(date) ? date : null;

  if (what === "min") return { min: date, max: null };
  else return { min: null, max: date };
}
github dlbnco / tripby.org / components / TimeMachine / index.js View on Github external
const isActive = () => {
    if (isValid(now) && isValid(startedAtDate) && isValid(endsAt)) {
      return isWithinInterval(now, {
        start: startedAtDate,
        end: endsAt,
      });
    }
    return false;
  };
github ProtonMail / proton-contacts / src / app / components / ContactKeysTable.js View on Github external
)
                                        }
                                    });
                                }
                            }
                        ].filter(Boolean);
                        const cells = [
                            <div title="{fingerprint}">
                                
                                <span>{fingerprint}</span>
                            </div>,
                            !isNarrow &amp;&amp; (isValid(creation) ? format(creation, 'PP', { locale: dateLocale }) : '-'),
                            !isTinyMobile &amp;&amp;
                                (isValid(expiration) ? format(expiration, 'PP', { locale: dateLocale }) : '-'),
                            !isNarrow &amp;&amp; algo,
                            
                                {isActive ? {c('Key badge').t`Active`} : null}
                                {isVerificationOnly ? (
                                    {c('Key badge').t`Verification only`}
                                ) : null}
                                {isWKD ? {c('Key badge').t`WKD`} : null}
                                {isTrusted ? {c('Key badge').t`Trusted`} : null}
                                {isRevoked ? {c('Key badge').t`Revoked`} : null}
                                {isExpired ? {c('Key badge').t`Expired`} : null}
                            ,
                            
                        ].filter(Boolean);
                        return ;
github Wikiki / bulma-calendar / src / js / datePicker / index.js View on Github external
_isValidDate(date, min, max) {
		try {
			if (!date) {
				return false;
			}
			if (dateFns.isValid(date)) {
				if (!min && !max) {
					return true;
				}
				if (min && max) {
					return dateFns.isWithinRange(date, min, max);
				}
				if (max) {
					return dateFns.isBefore(date, max) || dateFns.isEqual(date, max);
				}
				return dateFns.isAfter(date, min) || dateFns.isEqual(date, min);
			} else {
				return false;
			}
		} catch (e) {
			return false;
		}
github DanielYKPan / date-time-picker / src / picker.component.ts View on Github external
private parseToDate( val: any ): Date {
        if (!val) {
            return;
        }

        let parsedVal;
        if (typeof val === 'string') {
            parsedVal = parse(val, this.dateFormat, this.now);
        } else {
            parsedVal = val;
        }

        return isValid(parsedVal) ? parsedVal : null;
    }
github adarshpastakia / aurelia-ui-framework / src / calendar / calendar-utils.ts View on Github external
export const isBeforeMin = (month: Date, minDate: Date, n: number = 0) => {
  return isValid(minDate) ? isBefore(addMonths(startOfDay(month), n), startOfDay(minDate)) : false;
};
github davegomez / silky-charts / src / silky-charts / utils / buildLine.js View on Github external
    .x(({ name }) => xScale(isValid(name) ? new Date(name) : name))
    .y(({ value }) => yScale(value))
github nusmodifications / nusmods / website / src / utils / ical.ts View on Github external
export function iCalEventForExam(module: Module, semester: Semester): EventOption | null {
  const semesterData = getModuleSemesterData(module, semester);
  if (!semesterData) return null;

  const { examDate, examDuration } = semesterData;
  if (!examDate) return null;

  const start = new Date(examDate);
  if (!isValid(start)) return null;

  return {
    start,
    end: addMinutes(start, examDuration || DEFAULT_EXAM_DURATION),
    summary: `${module.moduleCode} Exam`,
    description: module.title,
  };
}
github adarshpastakia / aurelia-ui-framework / dist / commonjs / ui-calendar.js View on Github external
var isAfterMax = function (month, maxDate, n) {
    if (n === void 0) { n = 0; }
    return dateFns.isValid(maxDate) ? dateFns.isAfter(dateFns.addMonths(dateFns.startOfDay(month), n), dateFns.startOfDay(maxDate)) : false;
};
var isDisabled = function (config, date) {