date-fns#toDate TypeScript Examples
The following examples show how to use
date-fns#toDate.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: axisUtils.ts From anchor-web-app with Apache License 2.0 | 6 votes |
function checkTickPrint(i: number, length: number, timestamp: number): boolean {
const date = toDate(timestamp).getDate();
// always print
// if the tick is first of ticks
if (i === 0) {
return true;
}
// print 1 or 15
// if the tick is not near first or last
else if (date === 1) {
return i > 3 && i < length - 4;
}
return false;
}
Example #2
Source File: helpers.tsx From glide-frontend with GNU General Public License v3.0 | 6 votes |
getDateForBlock = async (currentBlock: number, block: number) => {
const blocksUntilBlock = block - currentBlock
const secondsUntilStart = blocksUntilBlock * ESC_BLOCK_TIME
// if block already happened we can get timestamp via .getBlock(block)
if (currentBlock > block) {
try {
const { timestamp } = await simpleRpcProvider.getBlock(block)
return toDate(timestamp * 1000)
} catch {
add(new Date(), { seconds: secondsUntilStart })
}
}
return add(new Date(), { seconds: secondsUntilStart })
}
Example #3
Source File: ngx-mat-datefns-date-adapter.ts From ngx-mat-datefns-date-adapter with MIT License | 6 votes |
deserialize(value: any): Date | null {
if (value) {
if (typeof value === 'string') {
if (this.options?.useUtc) {
return parseJSON(value);
}
return parseISO(value);
}
if (typeof value === 'number') {
return toDate(value);
}
if (value instanceof Date) {
return this.clone(value as Date);
}
return null;
}
return null;
}
Example #4
Source File: ngx-mat-datefns-date-adapter.ts From ngx-mat-datefns-date-adapter with MIT License | 6 votes |
parse(value: any, parseFormat: any): Date | null {
if (value) {
if (typeof value === 'string') {
if (this.options?.useUtc) {
const d = parse(value.trim(), parseFormat, new Date(), {
locale: this._dateFnsLocale,
});
return zonedTimeToUtc(d, UTC_TIMEZONE);
}
return parse(value.trim(), parseFormat, new Date(), {
locale: this._dateFnsLocale,
});
}
if (typeof value === 'number') {
return toDate(value);
}
if (value instanceof Date) {
return this.clone(value as Date);
}
return null;
}
return null;
}
Example #5
Source File: helpers.tsx From vvs-ui with GNU General Public License v3.0 | 6 votes |
getDateForBlock = async (currentBlock: number, block: number) => {
const blocksUntilBlock = block - currentBlock
const secondsUntilStart = blocksUntilBlock * CRONOS_BLOCK_TIME
// if block already happened we can get timestamp via .getBlock(block)
if (currentBlock > block) {
try {
const { timestamp } = await simpleRpcProvider.getBlock(block)
return toDate(timestamp * 1000)
} catch {
add(new Date(), { seconds: secondsUntilStart })
}
}
return add(new Date(), { seconds: secondsUntilStart })
}
Example #6
Source File: TimeFrame.tsx From glide-frontend with GNU General Public License v3.0 | 5 votes |
getFormattedDate = (timestamp: number) => {
const date = toDate(timestamp * 1000)
return format(date, 'MMM do, yyyy HH:mm')
}
Example #7
Source File: ngx-mat-datefns-date-adapter.ts From ngx-mat-datefns-date-adapter with MIT License | 5 votes |
clone(date: Date): Date {
return toDate(date);
}
Example #8
Source File: TimeFrame.tsx From vvs-ui with GNU General Public License v3.0 | 5 votes |
getFormattedDate = (timestamp: number) => {
const date = toDate(timestamp * 1000)
return format(date, 'MMM do, yyyy HH:mm')
}
Example #9
Source File: type_transformation.ts From Deep-Lynx with MIT License | 4 votes |
// convertValue will return a Conversion on successful or unsuccessful conversion, and null
// on values that need no conversion
static convertValue(dataType: string, value: any, date_conversion_format?: string): Conversion | null {
if (typeof value === 'undefined' || value === null || value === 'null') {
return new Conversion({original_value: value, errors: 'unable to convert value, value is null or undefined'});
}
switch (dataType) {
case 'number': {
if (typeof value === 'number') {
return null;
}
const convertedValue = parseInt(value, 10);
if (isNaN(convertedValue)) {
return new Conversion({original_value: value, errors: 'unable to convert value to number'});
}
return convertedValue % 1 === 0
? new Conversion({original_value: value, converted_value: Math.floor(convertedValue)})
: new Conversion({original_value: value, converted_value: convertedValue});
}
case 'float': {
if (typeof value === 'number') {
return null;
}
const convertedValue = parseFloat(value);
if (isNaN(convertedValue)) {
return new Conversion({original_value: value, errors: 'unable to convert value to float'});
}
return convertedValue % 1 === 0
? new Conversion({original_value: value, converted_value: Math.floor(convertedValue)})
: new Conversion({original_value: value, converted_value: convertedValue});
}
case 'number64': {
if (typeof value === 'string') {
return null;
}
return new Conversion({original_value: value, converted_value: String(value)});
}
case 'float64': {
if (typeof value === 'string') {
return null;
}
return new Conversion({original_value: value, converted_value: String(value)});
}
// because dates can be formatted in various ways, all we can really do for conversion is to
// set it to string - Deep Lynx only checks to see if dates are strings currently
case 'date': {
if (value instanceof Date) {
return new Conversion({original_value: value, converted_value: value.toISOString()});
}
// if it's a number we assume we're dealing with unix time
if (typeof value === 'number') {
return new Conversion({original_value: value, converted_value: toDate(value).toISOString()});
}
if (typeof value === 'string') {
try {
const convertedDate = date_conversion_format ? parse(value, date_conversion_format, new Date()) : new Date(value);
return new Conversion({original_value: value, converted_value: convertedDate.toISOString()});
} catch (e) {
return new Conversion({original_value: value, errors: `unable to convert value to date using format string: ${e}`});
}
}
return new Conversion({original_value: value, errors: 'unable to convert value to date, value is not string or number'});
}
case 'string': {
if (typeof value === 'string') {
return null;
}
return new Conversion({original_value: value, converted_value: String(value)});
}
case 'boolean': {
if (typeof value === 'boolean') {
return null;
}
if (typeof value === 'string') {
return new Conversion({
original_value: value,
converted_value: value.includes('true') || value.includes('TRUE') || value.includes('True') || value.includes('1'),
});
}
// only 1 dictates true here
if (typeof value === 'number') {
const converted_value = value === 1;
return new Conversion({original_value: value, converted_value});
}
// anything else dictates failure
return new Conversion({original_value: value, errors: 'unable to convert boolean, must be a boolean, string, or number to attempt conversion'});
}
// enumerations are currently string only values
case 'enumeration': {
if (typeof value === 'string') {
return null;
}
return new Conversion({original_value: value, converted_value: String(value)});
}
// files are generally filenames or URLs - as such, convert to string
case 'file': {
if (typeof value === 'string') {
return null;
}
return new Conversion({original_value: value, converted_value: String(value)});
}
case 'list': {
if (Array.isArray(value)) {
return null;
}
return new Conversion({original_value: value, converted_value: [value]});
}
// covers the "unknown" type where no conversion is necessary
default: {
return null;
}
}
}