js时间对象格式化 format(转载)

  1. /**
  2. * 时间对象的格式化
  3. */
  4. Date.prototype.format = function(format){
  5. /*
  6. * format="yyyy-MM-dd hh:mm:ss";
  7. */
  8. var o = {
  9. "M+": this.getMonth() + 1,
  10. "d+": this.getDate(),
  11. "h+": this.getHours(),
  12. "m+": this.getMinutes(),
  13. "s+": this.getSeconds(),
  14. "q+": Math.floor((this.getMonth() + 3) / 3),
  15. "S": this.getMilliseconds()
  16. }
  17. if (/(y+)/.test(format)) {
  18. format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 -
  19. RegExp.$1.length));
  20. }
  21. for (var k in o) {
  22. if (new RegExp("(" + k + ")").test(format)) {
  23. format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
  24. }
  25. }
  26. return format;
  27. }
时间: 2024-08-12 03:15:27

js时间对象格式化 format(转载)的相关文章

js 时间获取格式化 fmt

1 Date.prototype.format = function(fmt) { 2 var o = { 3 "M+" : this.getMonth()+1, //月份 4 "d+" : this.getDate(), //日 5 "h+" : this.getHours(), //小时 6 "m+" : this.getMinutes(), //分 7 "s+" : this.getSeconds()

js时间格式化详解

前言: js虽然提供了各种获取时间Date对象的不同属性方法,如:getDate 方法 | getDay 方法 | getFullYear 方法 | getHours 方法 ... ... 等等,但是却没有像java那样提供一个方法来供用户来根据自身提供的模板(pattern),来格式化指定时间对象,所以自己就封装了一个小方法,只供大家闲来调侃-.-,有好的建议还望慷慨指荐哦. 用到知识点: arguments:该对象代表正在执行的函数和调用它的函数的参数.不可显式创建,虽然有length属性,

js 时间操作 转载

Js获取当前日期时间及其它操作 var myDate = new Date();myDate.getYear();        //获取当前年份(2位)myDate.getFullYear();    //获取完整的年份(4位,1970-????)myDate.getMonth();       //获取当前月份(0-11,0代表1月)myDate.getDate();        //获取当前日(1-31)myDate.getDay();         //获取当前星期X(0-6,0代表

JS时间格式化出现2015-02-07 hh:12:30

1.问题背景 最近,做项目的过程中,遇到这样一个问题:利用JS对日期时间进行格式化时,页面出现了2015-02-07 hh:12:30,小时没有显示出数据:其中,hh换成HH时,有时小时位显示HH var dayTime = $("#endTime").val(); var dtime = new Date(dayTime); dtime.setTime(dtime.getTime()+365*100); var date = new Date(dtime.getTime()); $(

js时间格式化函数,支持Unix时间戳

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta charset="utf-8"> <title>js时间格式化函数,支持Unix时间戳</title> </head>

js实现倒计时及时间对象

JS实现倒计时效果代码如下: 1 <!doctype html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <title>无标题文档</title>h 6 <style> 7 #box { 8 width: 100%; 9 height: 400px; 10 background: black; 11 color: #fff; 12 font-size:4

使用date类和format类对系统当前时间进行格式化显示

一:Date------------String 代码1:(代码二对显示出来的时间格式进行优化) package DateDemo; import java.text.SimpleDateFormat; import java.util.Date; // 需求:将现在系统的时间打印出来 // 需要的类:Date 类:生成当前系统时间 // SimpleDateFormat 类:对生成的系统时间进行格式化 // 构造方法摘要 SimpleDateFormat() public class Date

js 获取时间对象代码

/** * 获取时间对象 */function getDateObj(addDayCount) { var dd = new Date(); dd.setDate(dd.getDate()+addDayCount);//获取addDayCount天后的日期 var y = dd.getFullYear(); var m = dd.getMonth()+1;//获取当前月份的日期 var d = dd.getDate(); var h = dd.getHours(); var M = dd.get

js时间格式化工具,时间戳格式化,字符串转时间戳

在开发中经常会用到时间格式化,有时候在网上搜索一大堆但不是自己想要的,自己总结一下,写一个时间格式化工具方便以后直接使用,欢迎大家来吐槽-- 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63