【问题标题】:How to get Online Current Date and Time in React Native如何在 React Native 中获取在线当前日期和时间
【发布时间】:2019-05-20 20:21:10
【问题描述】:

我试图通过这个命令获取当前时间和日期

new Date()

但是当我更改手机上的时间时,它会根据我手机上的时间而改变。

我想实时获取当前时间和日期。请帮我。感谢您的回复。

【问题讨论】:

    标签: android date react-native time


    【解决方案1】:

    你可以试试下面的代码

    fetchData() {
      var URL_REGISTER = 'https://www.google.com';
      fetch(URL_REGISTER, {method: 'POST',body: formData})
          .then(
              function(response) {
                  console.log(response.headers.get('Content-Type'));
                  console.log(response.headers.get('Date'));
    
                  console.log(response.status);
                  console.log(response.statusText);
                  console.log(response.type);
                  console.log(response.url);
                  if (response.status !== 200) {
                      console.log('Status Code: ' + response.status);
                      return;
                  }
    
                  // Examine the text in the response
                  response.json().then(function(data) {
                      console.log(data);
                  });
              }
          )
          .catch(function(err) {
              console.log('Fetch Error', err);
          });
    }
    

    参考:this stackoverflow link

    【讨论】:

      【解决方案2】:
      new Date().getTime();
      

      你可以使用任何你想替换 getTime() 到下面的:

      /** Returns a string representation of a date. The format of the string depends on the locale. */
      toString(): string;
      /** Returns a date as a string value. */
      toDateString(): string;
      /** Returns a time as a string value. */
      toTimeString(): string;
      /** Returns a value as a string value appropriate to the host environment's current locale. */
      toLocaleString(): string;
      /** Returns a date as a string value appropriate to the host environment's current locale. */
      toLocaleDateString(): string;
      /** Returns a time as a string value appropriate to the host environment's current locale. */
      toLocaleTimeString(): string;
      /** Returns the stored time value in milliseconds since midnight, January 1, 1970 UTC. */
      valueOf(): number;
      /** Gets the time value in milliseconds. */
      getTime(): number;
      /** Gets the year, using local time. */
      getFullYear(): number;
      /** Gets the year using Universal Coordinated Time (UTC). */
      getUTCFullYear(): number;
      /** Gets the month, using local time. */
      getMonth(): number;
      /** Gets the month of a Date object using Universal Coordinated Time (UTC). */
      getUTCMonth(): number;
      /** Gets the day-of-the-month, using local time. */
      getDate(): number;
      /** Gets the day-of-the-month, using Universal Coordinated Time (UTC). */
      getUTCDate(): number;
      /** Gets the day of the week, using local time. */
      getDay(): number;
      /** Gets the day of the week using Universal Coordinated Time (UTC). */
      getUTCDay(): number;
      /** Gets the hours in a date, using local time. */
      getHours(): number;
      /** Gets the hours value in a Date object using Universal Coordinated Time (UTC). */
      getUTCHours(): number;
      /** Gets the minutes of a Date object, using local time. */
      getMinutes(): number;
      /** Gets the minutes of a Date object using Universal Coordinated Time (UTC). */
      getUTCMinutes(): number;
      /** Gets the seconds of a Date object, using local time. */
      getSeconds(): number;
      /** Gets the seconds of a Date object using Universal Coordinated Time (UTC). */
      getUTCSeconds(): number;
      /** Gets the milliseconds of a Date, using local time. */
      getMilliseconds(): number;
      /** Gets the milliseconds of a Date object using Universal Coordinated Time (UTC). */
      getUTCMilliseconds(): number;
      /** Gets the difference in minutes between the time on the local computer and Universal Coordinated Time (UTC). */
      getTimezoneOffset(): number;
      

      【讨论】:

      • 如果我手动更改设备日期,它只会给我设备日期:(
      【解决方案3】:

      您可以实时向任何可靠的服务器发出请求。
      您将在服务器的响应中获得Date 标头(例如Date: Thu, 20 Dec 2018 10:33:14 GMT

      【讨论】:

        【解决方案4】:

        您可以像这样通过互联网使用 api 获取实时时间:

         https://timezonedb.com/api
        

        【讨论】:

        • 非常感谢,对我很有帮助!
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-10-03
        • 2019-08-13
        • 2017-10-05
        • 2013-10-15
        • 1970-01-01
        • 2015-03-22
        相关资源
        最近更新 更多