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

Docker Nginx container production and deployment implementation method

Quick Start 1. Find the nginx image on Docker Hub...

Vue implements multi-grid input box on mobile terminal

Recently, the company has put forward a requireme...

MySQL 8.0 can now handle JSON

Table of contents 1. Brief Overview 2. JSON basic...

How to modify mysql permissions to allow hosts to access

Enable remote access rights for mysql By default,...

Mysql Sql statement comments

You can add comments to MySQL SQL statements. Her...

Detailed explanation of the pitfalls of mixing MySQL order by and limit

In MySQL, we often use order by for sorting and l...

Sample code for implementing honeycomb/hexagonal atlas with CSS

I don’t know why, but UI likes to design honeycom...

14 Ways to Create Website Content That Engages Your Visitors

When I surf the Net, I often see web sites filled...

JavaScript code to implement Weibo batch unfollow function

A cool JavaScript code to unfollow Weibo users in...

Simple use of Vue vee-validate plug-in

Table of contents 1. Installation 2. Import 3. De...

Comparative Analysis of High Availability Solutions of Oracle and MySQL

Regarding the high availability solutions for Ora...

Detailed introduction and usage examples of map tag parameters

Map tags must appear in pairs, i.e. <map> .....

Understanding JavaScript prototype chain

Table of contents 1. Understanding the Equality R...