]> Untitled Git - lemmy-ui.git/commitdiff
Fixing timezone issue with date-fns. Fixes #1680 (#1681)
authorDessalines <dessalines@users.noreply.github.com>
Thu, 29 Jun 2023 03:09:25 +0000 (23:09 -0400)
committerGitHub <noreply@github.com>
Thu, 29 Jun 2023 03:09:25 +0000 (23:09 -0400)
Co-authored-by: Alec Armbruster <35377827+alectrocute@users.noreply.github.com>
package.json
src/shared/components/common/moment-time.tsx
src/shared/utils/helpers/format-past-date.ts
yarn.lock

index e5c1fa700de101562af00f15a3b3701d407704e8..2850984df1558cc2a2c5847231240dcfd7bf0b16 100644 (file)
@@ -52,6 +52,7 @@
     "cross-fetch": "^3.1.5",
     "css-loader": "^6.7.3",
     "date-fns": "^2.30.0",
+    "date-fns-tz": "^2.0.0",
     "emoji-mart": "^5.4.0",
     "emoji-short-name": "^2.0.0",
     "express": "~4.18.2",
index ec97eb499220054dec86a230ec945109be856b07..24bd3c79ed51319e782035860277af1c89ac9a97 100644 (file)
@@ -1,5 +1,5 @@
 import { capitalizeFirstLetter, formatPastDate } from "@utils/helpers";
-import format from "date-fns/format";
+import { formatInTimeZone } from "date-fns-tz";
 import parseISO from "date-fns/parseISO";
 import { Component } from "inferno";
 import { I18NextService } from "../../services";
@@ -13,7 +13,9 @@ interface MomentTimeProps {
 }
 
 function formatDate(input: string) {
-  return format(parseISO(input), "PPPPpppp");
+  const tz = Intl.DateTimeFormat().resolvedOptions().timeZone;
+  const parsed = parseISO(input + "Z");
+  return formatInTimeZone(parsed, tz, "PPPPpppp");
 }
 
 export class MomentTime extends Component<MomentTimeProps, any> {
index 78bc2a2d283d25ce88cb64031df44901e2e43dde..5bef4e83d1d0f678ed06958d3aafd95bc82465e0 100644 (file)
@@ -2,11 +2,8 @@ import formatDistanceStrict from "date-fns/formatDistanceStrict";
 import parseISO from "date-fns/parseISO";
 
 export default function (dateString?: string) {
-  return formatDistanceStrict(
-    parseISO(dateString ?? Date.now().toString()),
-    new Date(),
-    {
-      addSuffix: true,
-    }
-  );
+  const parsed = parseISO((dateString ?? Date.now().toString()) + "Z");
+  return formatDistanceStrict(parsed, new Date(), {
+    addSuffix: true,
+  });
 }
index 46fcba11d3ec2cd9d8a4d89cb654e59f1fe65845..57cde87a711c3544c0e56f3c2e5c88b4109b1a88 100644 (file)
--- a/yarn.lock
+++ b/yarn.lock
@@ -3237,6 +3237,11 @@ dashdash@^1.12.0:
   dependencies:
     assert-plus "^1.0.0"
 
+date-fns-tz@^2.0.0:
+  version "2.0.0"
+  resolved "https://registry.yarnpkg.com/date-fns-tz/-/date-fns-tz-2.0.0.tgz#1b14c386cb8bc16fc56fe333d4fc34ae1d1099d5"
+  integrity sha512-OAtcLdB9vxSXTWHdT8b398ARImVwQMyjfYGkKD2zaGpHseG2UPHbHjXELReErZFxWdSLph3c2zOaaTyHfOhERQ==
+
 date-fns@^2.30.0:
   version "2.30.0"
   resolved "https://registry.yarnpkg.com/date-fns/-/date-fns-2.30.0.tgz#f367e644839ff57894ec6ac480de40cae4b0f4d0"