Home > Web Front-end > JS Tutorial > body text

How to format timestamp function in javascript (with code)

不言
Release: 2018-08-28 17:18:21
Original
1720 people have browsed it

The content of this article is about how to format the timestamp function in JavaScript (with code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Pass in the timestamp and return the formatted string

 formatDate(v) {
          if (/^(-)?\d{1,10}$/.test(v))
           {
             v = v * 1000;
           } 
           else if (/^(-)?\d{1,13}$/.test(v))
            {
              v = v * 1;
            }               
            var dateObj = new Date(v);               
            var month = dateObj.getMonth() + 1;               
             var day =  dateObj.getDate();               
            var hours = dateObj.getHours();               
            var minutes = dateObj.getMinutes();               
            var seconds = dateObj.getSeconds();               
            if(month<10){
                   month = "0" + month;
               }               
            if(day<10){
                   day = "0" + day;
               }               
             if(hours<10){
                   hours = "0" + hours;
               }               
             if(minutes<10){
                   minutes = "0" + minutes;
               }               
             if(seconds<10){
                   seconds = "0" + seconds;
               }               
             var UnixTimeToDate = dateObj.getFullYear() + &#39;-&#39; + month + &#39;-&#39; +day + &#39; &#39; + hours + &#39;:&#39; + minutes + &#39;:&#39; + seconds;               
             return UnixTimeToDate;
            }
Copy after login

Related recommendations:

js formatted time and js formatted timestamp example_Basic knowledge

How to format time in js

The above is the detailed content of How to format timestamp function in javascript (with code). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template