vue3 timestamp conversion (without using filters)

vue3 timestamp conversion (without using filters)

When vue2 converts timestamps, it generally uses filters. After vue3, vue3 removes filters and can no longer be used. The official recommendation is to use methods or calculated properties.

A Time.ts file is written below, which can convert timestamps:

class Time {
    //Format time public formatTime(time: number) {
        let date = new Date(time * 1000);
        let year = date.getFullYear();
        let month = date.getMonth() + 1;
        let day = date.getDate();
        let hour = date.getHours();
        let minute = date.getMinutes();
        let second = date.getSeconds();
        return year + "-" + month.toString().padStart(2, "0") + "-" + day.toString().padStart(2, "0")
            + " " + hour.toString().padStart(2, "0") + ":" + minute.toString().padStart(2, "0")
            + ":" + second.toString().padStart(2, "0");
    }
}
 
const time = new Time();
export default time;


To use it, just call this method in the template and pass in the corresponding timestamp.

{{ time.formatTime(timestamp) }}


Display effect:

2021-05-17 10:59:59

Currently, it can only be converted into the above format. If more formats are required, you can add a second parameter to formatTime to indicate the formatted time format, and then convert accordingly.

This is the end of this article about vue3 timestamp conversion (without using filters). For more relevant vue3 timestamp conversion content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • The problem of this being undefined when using filters in Vue
  • Solve the problem that vue filters cannot get this object
  • Detailed explanation of Vue's data and event binding and filter filters
  • Vue defines private filters and basic usage
  • Solve the problem that vue local filter cannot get this

<<:  Problem of retrieving root password in MYSQL 5.7 under Linux (tested and available)

>>:  14 practical experiences on reducing SCSS style code by 50%

Recommend

Detailed tutorial on Docker pulling Oracle 11g image configuration

Without further ado Start recording docker pullin...

How to quickly modify the table structure of MySQL table

Quickly modify the table structure of a MySQL tab...

Use pure CSS to create a pulsating loader effect source code

Effect Preview Press the "Click to Preview&q...

MySQL index knowledge summary

The establishment of MySQL index is very importan...

This article will show you the principle of MySQL master-slave synchronization

Table of contents Brief Analysis of MySQL Master-...

Two ways to implement text stroke in CSS3 (summary)

question Recently I encountered a requirement to ...

CSS Back to Top Code Example

Most websites nowadays have long pages, some are ...

How to use linux commands to convert and splice audio formats

Install FFmpeg flac eric@ray:~$ sudo apt install ...

How to set Tomcat as an automatically started service? The quickest way

Set Tomcat to automatically start the service: I ...

Detailed steps to build a file server in Windows Server 2012

The file server is one of the most commonly used ...

Detailed example of MySQL joint table update data

1.MySQL UPDATE JOIN syntax In MySQL, you can use ...