date-fns
How to convert UTC time to EAST?
import { formatToTimeZone } from 'date-fns-timezone';
...
// From: 2020-10-02T05:43:00Z
// To: 2020-10-02 15:43:00
formatToTimeZone('2020-10-02T05:43:00Z', 'YYYY-MM-DD HH:mm:ss', { timeZone: 'Australia/Melbourne' });
How to convert time string to Date instance?
// Date time needs to have this format: 'YYYY-MM-DD HH:mm:ss' new Date(yourString)
How to convert string to plain Javascript time object and vice versa?
import { parse } from 'date-fns';
// "2021-10-04 16:59:03.000" to "3368473269"
const comparedDate = parse(
yourString,
'yyyy-MM-dd HH:mm:ss.SSS',
new Date(),
).getTime();
import { format } from 'date-fns';
// "3368473269" to "2021-10-04 16:59:03.000"
const comparedDate = format(timeObjString, 'yyyy-MM-dd HH:mm:ss.SSS');
How to get seconds difference between 2 date time?
// Between future and current time differenceInSeconds(new Date(futureTimeString), new Date())
How to format today date?
import { format } from 'date-fns';
const today = format(new Date(), 'yyyy-MM-dd') // 2021-02-12
How to convert date string to fns date?
import { parse } from 'date-fns';
const fnsDate = parse('2021-02-15', 'yyyy-MM-dd', new Date());
How to get day difference between 2 date strings?
import { format, differenceInDays, parse } from 'date-fns';
// 2021-09-10 and today
const comparedDate = parse('2021-09-10', 'yyyy-MM-dd', new Date());
const todayWithoutTime = new Date(
new Date().getFullYear(),
new Date().getMonth(),
new Date().getDate(),
);
const dayDifference = differenceInDays(comparedDate, todayWithoutTime);
if (dayDifference === 1) {
console.log('Tomorrow')
} else {
console.log(dayDifference)
}
momentJs
How to format current date?
moment(new Date()).format("DD/MM/YYYY")
How to format current date to UTC?
import moment from 'moment-timezone'
export const getCurrentUTCTime = () => {
const utcMoment = moment.utc().format('YYYY-MM-DDTHH:mm:ss')
return utcMoment + 'Z'
}
How to convert UTC back to local time?
import moment from 'moment-timezone'
export const convertUTCtoLocalFormat = (utcTime: string, format: string) => {
const momentTime = moment(utcTime)
return momentTime.tz('Australia/Melbourne').format(format)
}
How to format date string?
export const formatDate = (
dateTime: string,
inputFormat: string,
outputFormat: string
) => {
const mom = moment(dateTime, inputFormat) // 2021-08-21
return mom.format(outputFormat) // 02 Aug 21
}
How to convert string to moment time
// Normal time string const momentTime = Moment(strTime); // UTC time string const momentTime = Moment.utc(strTimeUtc);
Compare and sort moment time array
allTimeTable.sort((a, b) => {
const aTime = Moment.utc(a.utc_time);
const bTime = Moment.utc(b.utc_time);
if (aTime.isBefore(bTime)) {
return -1;
} else if (aTime.isAfter(bTime)) {
return 1;
}
return 0;
});
How to calculate time difference in hours?
const startTime = Moment(reduxLastUpdate);
const endTime = Moment();
const duration = Moment.duration(endTime.diff(startTime));
const hours = duration.asHours();
How to calculate days difference?
var a = moment([2007, 0, 29]); var b = moment([2007, 0, 28]); a.diff(b, 'days')
Plain JavaScript
How to convert UTC time to unix?
Date.parse(utcTimeString)
How to add 2 hours to Date object?
const now = new Date(); now.setHours( now.getHours() + 2 ); // 1589857034624
// Test new Date(1589857034624)
This solution can’t be applied to float value. This means you can’t use “now.getHours() + 1.5” => This still returns 1 hour, not 1.5 hour
How to add minutes to Date object?
const now = new Date(); const next30mins = new Date(now.getTime() + 30*60000);
How to create a timer countdown?
Create a 60 seconds countdown timer
let timeLeft = 60
const timer = setInterval(function(){
if (timeLeft <= 0) {
clearInterval(timer)
}
document.getElementById("countdown-text").innerHTML = timeLeft
timeLeft -= 1
}, 1000)
Leave a Reply