更改区域设置时间js

问题描述 投票:0回答:1

如何更改日期本地化并显示新的本地化日期?

const Locale = () => {
  return (
    <div>
      <button onClick={() => moment.locale("en")}>English</button>
      <button onClick={() => moment.locale("de")}>German</button>
      <p>{moment().format("LLLL")}</p>
    </div>
  );
};

codesandbox示例https://codesandbox.io/s/vigorous-violet-v0hvf

reactjs momentjs
1个回答
0
投票
const deMoment = moment().locale("de").format("LLLL"); const enMoment = moment().locale("en").format("LLLL"); const Locale = () => { const [momentDate, setMomentDate] = useState(deMoment); useEffect(() => {}, [momentDate]); return ( <div> <button onClick={() => setMomentDate(enMoment)}>English</button> <button onClick={() => setMomentDate(deMoment)}>German</button> <p>{momentDate}</p> </div> ); };
© www.soinside.com 2019 - 2024. All rights reserved.