Home Web Front-end JS Tutorial Detailed example of React Native time conversion format tool class

Detailed example of React Native time conversion format tool class

May 12, 2018 am 09:23 AM
native react time

本文主要为大家分享了React Native时间转换格式工具类,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能帮助到大家。

本文实例为大家分享了React Native时间转换格式工具类,供大家参考,具体内容如下

class DateUtil{
  /**
   * 例如:2017-06-28 10:48:46转成date类,
   * 可把- replace成/
   * @param dateString
   * @return Date
   */
  static parserDateString(dateString){
    if(dateString){
      let regEx = new RegExp("\\-","gi");
      let validDateStr=dateString.replace(regEx,"/");
      let milliseconds=Date.parse(validDateStr);
      return new Date(milliseconds);

    }
  }

  // timestamp时间戳 formater时间格式
  static formatDate(timestamp, formater) { 
    let date = new Date();
    date.setTime(parseInt(timestamp));
    formater = (formater != null)? formater : 'yyyy-MM-dd hh:mm';
    Date.prototype.Format = function (fmt) {
      var o = {
        "M+": this.getMonth() + 1, //月
        "d+": this.getDate(), //日
        "h+": this.getHours(), //小时
        "m+": this.getMinutes(), //分
        "s+": this.getSeconds(), //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds() //毫秒
      };

      if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
      for (var k in o) {
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ?
          (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
      }
      return fmt;
    }
    return date.Format(formater);
  }
}
export default DateUtil;
Copy after login

外界调用方法

// 可将 - 替换成 / 或者 年月日等
DateUtil.formatDate(时间戳, "yyyy-MM-dd hh:mm:ss")
Copy after login

相关推荐:

React Native react-navigation 导航使用详解

实例详解VSCode 配置React Native开发环境

React Native模块Permissions权限申请

The above is the detailed content of Detailed example of React Native time conversion format tool class. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1657
14
PHP Tutorial
1257
29
C# Tutorial
1230
24
How to set the time for publishing works on Xiaohongshu? Is the time for publishing the work accurate? How to set the time for publishing works on Xiaohongshu? Is the time for publishing the work accurate? Mar 24, 2024 pm 01:31 PM

Xiaohongshu, a platform full of life and knowledge sharing, allows more and more creators to express their opinions freely. In order to get more attention and likes on Xiaohongshu, in addition to the quality of content, the time of publishing works is also crucial. So, how to set the time for Xiaohongshu to publish works? 1. How to set the time for publishing works on Xiaohongshu? 1. Understand the active time of users. First, it is necessary to clarify the active time of Xiaohongshu users. Generally speaking, 8 pm to 10 pm and weekend afternoons are the times when user activity is high. However, this time period will also vary depending on factors such as audience group and geography. Therefore, in order to better grasp the active period of users, it is recommended to conduct a more detailed analysis of the behavioral habits of different groups. By understanding users’ lives

PHP, Vue and React: How to choose the most suitable front-end framework? PHP, Vue and React: How to choose the most suitable front-end framework? Mar 15, 2024 pm 05:48 PM

PHP, Vue and React: How to choose the most suitable front-end framework? With the continuous development of Internet technology, front-end frameworks play a vital role in Web development. PHP, Vue and React are three representative front-end frameworks, each with its own unique characteristics and advantages. When choosing which front-end framework to use, developers need to make an informed decision based on project needs, team skills, and personal preferences. This article will compare the characteristics and uses of the three front-end frameworks PHP, Vue and React.

Integration of Java framework and front-end React framework Integration of Java framework and front-end React framework Jun 01, 2024 pm 03:16 PM

Integration of Java framework and React framework: Steps: Set up the back-end Java framework. Create project structure. Configure build tools. Create React applications. Write REST API endpoints. Configure the communication mechanism. Practical case (SpringBoot+React): Java code: Define RESTfulAPI controller. React code: Get and display the data returned by the API.

Vue.js vs. React: Project-Specific Considerations Vue.js vs. React: Project-Specific Considerations Apr 09, 2025 am 12:01 AM

Vue.js is suitable for small and medium-sized projects and fast iterations, while React is suitable for large and complex applications. 1) Vue.js is easy to use and is suitable for situations where the team is insufficient or the project scale is small. 2) React has a richer ecosystem and is suitable for projects with high performance and complex functional needs.

How long can a Douyin short video be? How to increase the number of views on TikTok? How long can a Douyin short video be? How to increase the number of views on TikTok? Mar 16, 2024 pm 04:20 PM

In the current craze of social media, Douyin, as a popular short video platform, has attracted countless users to show their creativity and share their lives on its platform. However, the duration limit of Douyin short videos has become one of the focuses of users. So, how long can a short video on Douyin not exceed? Next, we’ll delve into this issue and share some ways on how to increase your TikTok views. 1. How long can a Douyin short video be? According to the regulations of the Douyin platform, when ordinary users upload videos, the length of a single video cannot exceed 60 seconds. This time limit is designed to encourage users to create concise and interesting short video content to cater to users’ needs for fast-paced and efficient information acquisition. This is also one of the features of Douyin as a short video platform.

React's Role in HTML: Enhancing User Experience React's Role in HTML: Enhancing User Experience Apr 09, 2025 am 12:11 AM

React combines JSX and HTML to improve user experience. 1) JSX embeds HTML to make development more intuitive. 2) The virtual DOM mechanism optimizes performance and reduces DOM operations. 3) Component-based management UI to improve maintainability. 4) State management and event processing enhance interactivity.

Why doesn't the time show when posting works on Douyin? What should I do if the time is not displayed when publishing a work? Why doesn't the time show when posting works on Douyin? What should I do if the time is not displayed when publishing a work? Mar 21, 2024 pm 10:01 PM

Douyin, this short video platform has penetrated deeply into our lives and has become an important way for us to record our lives and share our happiness. However, recently some users have found that the time is not displayed when they publish their works, which makes them confused. So, why doesn’t the time show when Douyin releases works? 1. Why doesn’t the time show when posting works on Douyin? The time is not displayed for works published on Douyin, which may be due to the following reasons: 1. The length of the work is too short: Douyin requires the work to be more than 15 seconds long. If the work is less than 15 seconds long, the system may not display the time. 2. Work format issues: Douyin only supports common video formats such as MP4 and MOV. If the format uploaded by the user is not supported, the time may not be displayed. 3. Software version problem: partially used

React vs. Vue: Which Framework Does Netflix Use? React vs. Vue: Which Framework Does Netflix Use? Apr 14, 2025 am 12:19 AM

Netflixusesacustomframeworkcalled"Gibbon"builtonReact,notReactorVuedirectly.1)TeamExperience:Choosebasedonfamiliarity.2)ProjectComplexity:Vueforsimplerprojects,Reactforcomplexones.3)CustomizationNeeds:Reactoffersmoreflexibility.4)Ecosystema

See all articles