Vue converts a timestamp to a custom time format.

Source: Internet
Author: User

Vue converts a timestamp to a custom time format.

1. First create a date. js file and write the following code:

Export function formatDate (date, fmt) {if (/(y + )/. test (fmt) {fmt = fmt. replace (RegExp. $1, (date. getFullYear () + ''). substr (4-RegExp. $1. length);} let o = {'m + ': date. getMonth () + 1, 'D + ': date. getDate (), 'H + ': date. getHours (),'m + ': date. getMinutes (),'s + ': date. getSeconds ()}; for (let k in o) {if (new RegExp ('($ {k })'). test (fmt) {let str = o [k] + ''; fmt = fmt. replace (RegExp. $1, (RegExp. $1. length = 1 )? Str: padLeftZero (str) ;}return fmt ;}; function padLeftZero (str) {return ('00' + str). substr (str. length );};

2. Introduce the date. js file on the page to be converted:

Import {formatDate} from '../date. js ';

3. The call method is as follows:

FormatDate (new Date (time * 1000), 'yyyy-MM-dd hh: mm ');

The preceding vue's method of converting the timestamp into a custom time format is to share all the content with you. I hope you can give us a reference, and hope you can also provide more support for the customer's house.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.