简体   繁体   English

日期和时间(两个字段)转换为 UTC

[英]Date and time (two fields) convert into UTC

I have two fields:我有两个字段:

  • Date日期
  • Time时间

and I have managed to show/display dates in user's time zone, but store data in UTC (in state) because I want to send API request and store all times in UTC.我已经设法在用户的时区中显示/显示日期,但以 UTC(状态)存储数据,因为我想发送 API 请求并以 UTC 存储所有时间。

However, I have a problem when I select:但是,我在选择时遇到了问题:

  • 7th September 2020 2020 年 9 月 7 日
  • 00:30 00:30

This means that I need to set 6th September 22:30 (if we assume that my time zone is Europe/Berlin.这意味着我需要设置 9 月 6 日 22:30(如果我们假设我的时区是欧洲/柏林。

This is how I display date:这是我显示日期的方式:

    const date = moment.utc(selected).tz(localStorage.getItem('format_timezone')).format('YYYY-MM-DD');
    const newSelected = selected ? new Date(date) : '';

and this is how I display time:这就是我显示时间的方式:

 const date = moment.utc().format('YYYY/MM/DD');
    const time = moment.utc(`${date} ${selected}`).tz(localStorage.getItem('format_timezone')).format('YYYY/MM/DD HH:mm:ss');
    const newSelected = selected ? new Date(time) : '';

and this is how I handle input change:这就是我处理输入更改的方式:

handleDateInput(e) {
        const { setCampaignInputs } = this.props;
        setCampaignInputs('date_input', moment(e).format(convertToMoment('Y-m-d')));
    }

    handleTimeInput(e) {
        const { setCampaignInputs } = this.props;
        setCampaignInputs('time_input', moment.utc(e).format(convertToMoment('H:i:s')));
    }

Any tips how to handle this if I store data in two field?如果我将数据存储在两个字段中,任何提示如何处理这个问题? It would be easy if I store data in one input field...如果我将数据存储在一个输入字段中会很容易...

Thanks!谢谢!

The best way to deal with date/time in javascript is by using epochMilli.在 javascript 中处理日期/时间的最佳方法是使用 epochMilli。 From epochMilli to date using new Date(epochMilli) whatever timezone the browsers are.从 epochMilli 到使用 new Date(epochMilli) 的日期,无论浏览器是什么时区。 And From Date to epochMilli is by using getTime() method.从日期到 epochMilli 是通过使用 getTime() 方法。

How about this:这个怎么样:

On user-input you'll have the inputDate and inputTime strings.在用户输入时,您将拥有inputDateinputTime字符串。 That same string can be utilized to generate the state value and also the display value (which will be rendered from state)相同的字符串可用于生成状态值和显示值(将从状态呈现)

 const inputDate = '2020-01-01' const inputTime = '00:30' const stateValue = moment(`${inputDate} ${inputTime}`) .utc() .format('YYYY-MM-DD HH:mm') const displayDateTime = moment(`${stateValue}Z`) .format('YYYY-MM-DD HH:mm') const displayDate = moment(`${stateValue}Z`).format('YYYY-MM-DD') const displayTime = moment(`${stateValue}Z`).format('HH:mm'); console.log('state-value : ', stateValue) // state value console.log('display-date-time : ', displayDateTime) // date-time display value console.log('display-date : ', displayDate) // date display value console.log('display-time : ', displayTime) // time display value
 <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.28.0/moment.min.js"></script>

Good Luck...祝你好运...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM