Home Web Front-end JS Tutorial How to format time in js

How to format time in js

Mar 21, 2018 am 10:40 AM
javascript time format

这次给大家带来js如何格式化时间日期,下面就是实战案例,一起来看一下。

// 对Date的扩展,将 Date 转化为指定格式的String
// 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符,
 // 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字) 
 // 例子:
  // (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423 
  // (new Date()).Format("yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18
   Date.prototype.Format = function (fmt) { //author: meizz 
    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;   
}
//调用:
 var time1 = new Date().Format("yyyy-MM-dd");
 var time2 = new Date().Format("yyyy-MM-dd hh:mm:ss");
Copy after login

 相关推荐:

JS 时间格式化  

HTML时间格式化

php时间格式化 

The above is the detailed content of How to format time in js. 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 Article Tags

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)

How to format c drive with dos command How to format c drive with dos command Feb 19, 2024 pm 04:23 PM

How to format c drive with dos command

How much does a Douyin level 10 light sign cost? How many days does it take to create a level 10 fan sign? How much does a Douyin level 10 light sign cost? How many days does it take to create a level 10 fan sign? Mar 11, 2024 pm 05:37 PM

How much does a Douyin level 10 light sign cost? How many days does it take to create a level 10 fan sign?

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

How to set the time for publishing works on Xiaohongshu? Is the time for publishing the work accurate?

How long does it take to clear the Elden Ring? How long does it take to clear the Elden Ring? Mar 11, 2024 pm 12:50 PM

How long does it take to clear the Elden Ring?

Detailed explanation of Linux file time viewing techniques Detailed explanation of Linux file time viewing techniques Feb 21, 2024 pm 01:15 PM

Detailed explanation of Linux file time viewing techniques

Will formatting a laptop make it faster? Will formatting a laptop make it faster? Feb 12, 2024 pm 11:54 PM

Will formatting a laptop make it faster?

Revealed secrets of cell phone format recovery methods (mobile phone malfunction? Don't worry) Revealed secrets of cell phone format recovery methods (mobile phone malfunction? Don't worry) May 04, 2024 pm 06:01 PM

Revealed secrets of cell phone format recovery methods (mobile phone malfunction? Don't worry)

How to adjust the time on gshock watch How to adjust the time on gshock watch Feb 21, 2024 pm 09:04 PM

How to adjust the time on gshock watch

See all articles