Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

fix compare convert date to utc format

kruzhambusdeea95076ba9
diff --git a/src/pages/Project/View/ViewProject.tsx b/src/pages/Project/View/ViewProject.tsx
index a4161b72c..a675b2213 100644
--- a/src/pages/Project/View/ViewProject.tsx
+++ b/src/pages/Project/View/ViewProject.tsx
@@ -35,6 +35,7 @@ import _truncate from 'lodash/truncate'
import PropTypes from 'prop-types'
import * as SwetrixSDK from '@swetrix/sdk'
+import { periodToCompareDate } from 'utils/compareConvertDate'
import { getTimeFromSeconds, getStringFromTime } from 'utils/generic'
import { getItem, setItem } from 'utils/localstorage'
import Title from 'components/Title'
@@ -43,7 +44,7 @@ import {
tbPeriodPairs, getProjectCacheKey, LIVE_VISITORS_UPDATE_INTERVAL, DEFAULT_TIMEZONE, CDN_URL, isDevelopment,
timeBucketToDays, getProjectCacheCustomKey, roleViewer, MAX_MONTHS_IN_PAST, PROJECT_TABS,
TimeFormat, getProjectForcastCacheKey, chartTypes, roleAdmin, TRAFFIC_PANELS_ORDER, PERFORMANCE_PANELS_ORDER, isSelfhosted, tbPeriodPairsCompare,
- PERIOD_PAIRS_COMPARE, periodToCompareDate, filtersPeriodPairs, IS_ACTIVE_COMPARE,
+ PERIOD_PAIRS_COMPARE, filtersPeriodPairs, IS_ACTIVE_COMPARE,
} from 'redux/constants'
import { IUser } from 'redux/models/IUser'
import { IProject, ILiveStats } from 'redux/models/IProject'
@@ -484,11 +485,16 @@ const ViewProject = ({
let key = ''
let keyCompare = ''
let from
- let fromCompare
+ let fromCompare: string | undefined
let to
- let toCompare
+ let toCompare: string | undefined
let customEventsChart = customEventsChartData
+ // das
+ // sad
+ // asd
+ const today = dayjs.utc().toDate()
+ console.log('today', today)
if (isActiveCompare) {
if (dateRangeCompare && activePeriodCompare === PERIOD_PAIRS_COMPARE.CUSTOM) {
fromCompare = getFormatDate(dateRangeCompare[0])
@@ -503,8 +509,8 @@ const ViewProject = ({
}
if (date) {
- fromCompare = getFormatDate(date.from)
- toCompare = getFormatDate(date.to)
+ fromCompare = date.from
+ toCompare = date.to
keyCompare = getProjectCacheCustomKey(fromCompare, toCompare, timeBucket)
}
}
@@ -1482,7 +1488,7 @@ const ViewProject = ({
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [isActiveCompare, activePeriodCompare, dateRangeCompare])
- const compareDisable = () => {
+ function compareDisable() {
setIsActiveCompare(false)
setDateRangeCompare(null)
setDataChartCompare({})
diff --git a/src/redux/constants/index.ts b/src/redux/constants/index.ts
index 969274ae0..9122e0abb 100644
--- a/src/redux/constants/index.ts
+++ b/src/redux/constants/index.ts
@@ -106,93 +106,6 @@ export const PERIOD_PAIRS_COMPARE: {
DISABLE: 'disable',
}
-export const periodToCompareDate: {
- period: string
- formula: (date?: Date[]) => {
- from: Date
- to: Date
- }
-}[] = [{
- period: '1d',
- formula: () => {
- const to = new Date()
- to.setDate(to.getDate() - 1)
- const from = new Date()
- from.setDate(from.getDate() - 2)
- return {
- from,
- to,
- }
- },
-}, {
- period: '7d',
- formula: () => {
- const to = new Date()
- to.setDate(to.getDate() - 7)
- const from = new Date()
- from.setDate(from.getDate() - 14)
- return {
- from,
- to,
- }
- },
-}, {
- period: '4w',
- formula: () => {
- const to = new Date()
- to.setDate(to.getDate() - 28)
- const from = new Date()
- from.setDate(from.getDate() - 56)
- return {
- from,
- to,
- }
- },
-}, {
- period: '3M',
- formula: () => {
- const to = new Date()
- to.setMonth(to.getMonth() - 3)
- const from = new Date()
- from.setMonth(from.getMonth() - 6)
- return {
- from,
- to,
- }
- },
-}, {
- period: '12M',
- formula: () => {
- const to = new Date()
- to.setMonth(to.getMonth() - 12)
- const from = new Date()
- from.setMonth(from.getMonth() - 24)
- return {
- from,
- to,
- }
- },
-}, {
- period: 'custom',
- formula: (date) => {
- if (!date) {
- return {
- from: new Date(),
- to: new Date(),
- }
- }
- const days = (date[1].getTime() - date[0].getTime()) / (1000 * 3600 * 24)
- const to = new Date()
- to.setDate(to.getDate() - days)
- const from = new Date()
- from.setDate(from.getDate() - days * 2)
- return {
- from,
- to,
- }
- },
-}]
-
interface IStringObject {
[key: string]: string
}
diff --git a/src/utils/compareConvertDate.ts b/src/utils/compareConvertDate.ts
new file mode 100644
index 000000000..a38691e96
--- /dev/null
+++ b/src/utils/compareConvertDate.ts
@@ -0,0 +1,87 @@
+import dayjs from 'dayjs'
+import utc from 'dayjs/plugin/utc'
+
+import { getFormatDate } from '../pages/Project/View/ViewProject.helpers'
+
+dayjs.extend(utc)
+
+export const periodToCompareDate: {
+ period: string
+ formula: (date?: Date[]) => {
+ from: string
+ to: string
+ }
+}[] = [{
+ period: '1d',
+ formula: () => {
+ const to = dayjs.utc().subtract(1, 'day').format('YYYY-MM-DD')
+ const from = dayjs.utc().subtract(2, 'day').format('YYYY-MM-DD')
+
+ return {
+ from,
+ to,
+ }
+ },
+}, {
+ period: '7d',
+ formula: () => {
+ const to = dayjs.utc().subtract(7, 'day').format('YYYY-MM-DD')
+ const from = dayjs.utc().subtract(14, 'day').format('YYYY-MM-DD')
+
+ return {
+ from,
+ to,
+ }
+ },
+}, {
+ period: '4w',
+ formula: () => {
+ const to = dayjs.utc().subtract(28, 'day').format('YYYY-MM-DD')
+ const from = dayjs.utc().subtract(56, 'day').format('YYYY-MM-DD')
+
+ return {
+ from,
+ to,
+ }
+ },
+}, {
+ period: '3M',
+ formula: () => {
+ const to = dayjs.utc().subtract(3, 'month').format('YYYY-MM-DD')
+ const from = dayjs.utc().subtract(6, 'month').format('YYYY-MM-DD')
+
+ return {
+ from,
+ to,
+ }
+ },
+}, {
+ period: '12M',
+ formula: () => {
+ const to = dayjs.utc().subtract(12, 'month').format('YYYY-MM-DD')
+ const from = dayjs.utc().subtract(24, 'month').format('YYYY-MM-DD')
+
+ return {
+ from,
+ to,
+ }
+ },
+}, {
+ period: 'custom',
+ formula: (date) => {
+ if (!date) {
+ return {
+ from: getFormatDate(new Date()),
+ to: getFormatDate(new Date()),
+ }
+ }
+ const days = (date[1].getTime() - date[0].getTime()) / (1000 * 3600 * 24)
+ const to = dayjs.utc().subtract(days, 'day').format('YYYY-MM-DD')
+ const from = dayjs.utc().subtract(days * 2, 'day').format('YYYY-MM-DD')
+
+ return {
+ from,
+ to,
+ }
+ },
+}]