@ -1,43 +1,27 @@
import dayjs from 'dayjs' ;
import utc from 'dayjs/plugin/utc' ;
dayjs . extend ( utc )
// https://stackoverflow.com/a/50130338
export function formatISO ( date ) {
return new Date ( date . getTime ( ) - ( date . getTimezoneOffset ( ) * 60000 ) )
. toISOString ( )
. split ( "T" ) [ 0 ] ;
return date . format ( 'YYYY-MM-DD' )
}
export function shiftMonths ( date , months ) {
const newDate = new Date ( date . getTime ( ) )
const d = newDate . getDate ( ) ;
newDate . setMonth ( newDate . getMonth ( ) + + months ) ;
if ( newDate . getDate ( ) != d ) {
newDate . setDate ( 0 ) ;
}
return newDate ;
return date . add ( months , 'months' )
}
export function shiftDays ( date , days ) {
const newDate = new Date ( date . getTime ( ) )
newDate . setDate ( newDate . getDate ( ) + days )
return newDate
return date . add ( days , 'days' )
}
const MONTHS = [
"January" , "February" , "March" ,
"April" , "May" , "June" , "July" ,
"August" , "September" , "October" ,
"November" , "December"
]
const DAYS _ABBREV = [
"Sun" , "Mon" , "Tue" , "Wed" , "Thu" , "Fri" , "Sat"
]
export function formatMonthYYYY ( date ) {
return ` ${ MONTHS [ date . getMonth ( ) ] } ${ date . getFullYear ( ) } ` ;
return date . format ( 'MMMM YYYY' )
}
export function formatYear ( date ) {
return ` Year of ${ date . getFullY ear( ) } ` ;
return ` Year of ${ date . year ( ) } ` ;
}
export function formatYearShort ( date ) {
@ -45,41 +29,27 @@ export function formatYearShort(date) {
}
export function formatDay ( date ) {
var weekday = DAYS _ABBREV [ date . getDay ( ) ] ;
if ( date . getFullYear ( ) !== ( new Date ( ) ) . getFullYear ( ) ) {
return ` ${ weekday } , ${ date . getDate ( ) } ${ formatMonthShort ( date ) } ${ date . getFullYear ( ) } ` ;
if ( date . year ( ) !== dayjs ( ) . year ( ) ) {
return date . format ( 'ddd, DD MMM YYYY' )
} else {
return ` ${ weekday } , ${ date . getDate ( ) } ${ formatMonthShort ( date ) } ` ;
return date . format ( 'ddd, DD MMM' )
}
}
export function formatDayShort ( date , includeYear = false ) {
let formatted = ` ${ date . getDate ( ) } ${ formatMonthShort ( date ) } `
if ( includeYear ) {
formatted += ` ${ formatYearShort ( date ) } `
}
return formatted
}
export function parseUTCDate ( dateString ) {
var date ;
// Safari Compatibility
if ( typeof dateString === "string" && dateString . includes ( ' ' ) ) {
const parts = dateString . split ( /[^0-9]/ ) ;
parts [ 1 ] -= 1 ;
date = new Date ( ... parts ) ;
if ( includeYear ) {
return date . format ( 'D MMM YY' )
} else {
date = new Date ( dateString ) ;
return date . format ( 'D MMM' )
}
}
return new Date ( date . getTime ( ) + date . getTimezoneOffset ( ) * 60000 ) ;
export function parseUTCDate ( dateString ) {
return dayjs . utc ( dateString )
}
// https://stackoverflow.com/a/11124448
export function nowForSite ( site ) {
const browserOffset = ( new Date ( ) ) . getTimezoneOffset ( ) * 60
return new Date ( new Date ( ) . getTime ( ) + ( site . offset * 1000 ) + ( browserOffset * 1000 ) )
return dayjs . utc ( ) . utcOffset ( site . offset / 60 )
}
export function lastMonth ( site ) {
@ -99,43 +69,39 @@ export function isThisMonth(site, date) {
}
export function isThisYear ( site , date ) {
return date . getFullYear( ) === nowForSite ( site ) . getFullY ear( )
return date . year( ) === nowForSite ( site ) . y ear( )
}
export function isBefore ( date1 , date2 , period ) {
/* assumes 'day' and 'month' are the only valid periods */
if ( date1 . getFullYear( ) !== date2 . getFullY ear( ) ) {
return date1 . getFullYear( ) < date2 . getFullY ear( ) ;
if ( date1 . year( ) !== date2 . y ear( ) ) {
return date1 . year( ) < date2 . y ear( ) ;
}
if ( period === "year" ) {
return false ;
}
if ( date1 . getMonth( ) !== date2 . getM onth( ) ) {
return date1 . getMonth( ) < date2 . getM onth( ) ;
if ( date1 . month( ) !== date2 . m onth( ) ) {
return date1 . month( ) < date2 . m onth( ) ;
}
if ( period === "month" ) {
return false ;
}
return date1 . getDate( ) < date2 . getD ate( )
return date1 . date( ) < date2 . d ate( )
}
export function isAfter ( date1 , date2 , period ) {
/* assumes 'day' and 'month' are the only valid periods */
if ( date1 . getFullYear( ) !== date2 . getFullY ear( ) ) {
return date1 . getFullYear( ) > date2 . getFullY ear( ) ;
if ( date1 . year( ) !== date2 . y ear( ) ) {
return date1 . year( ) > date2 . y ear( ) ;
}
if ( period === "year" ) {
return false ;
}
if ( date1 . getMonth( ) !== date2 . getM onth( ) ) {
return date1 . getMonth( ) > date2 . getM onth( ) ;
if ( date1 . month( ) !== date2 . m onth( ) ) {
return date1 . month( ) > date2 . m onth( ) ;
}
if ( period === "month" ) {
return false ;
}
return date1 . getDate ( ) > date2 . getDate ( )
}
function formatMonthShort ( date ) {
return ` ${ MONTHS [ date . getMonth ( ) ] . substring ( 0 , 3 ) } ` ;
return date1 . date ( ) > date2 . date ( )
}