javascript格式化日期时间方法汇总


Posted in Javascript onJune 19, 2015

示例一:

<!DOCTYPE html>
<html>
<head>
 <meta charset="UTF-8">
 <title>TT</title>
 <script src="jquery.js"></script>
</head>
<body>
<p style="line-height: 50%;"><span></span></p>
<input type="button" value="轻轻" onclick="syncTime()">
<script>
  function syncTime(){
    var mydate = new Date();
    var week;
    switch (mydate.getDay()){
    case 1: week="星期一"; break;
    case 2: week="星期二"; break;
    case 3: week="星期三"; break;
    case 4: week="星期四"; break;
    case 5: week="星期五"; break;
    case 6: week="星期六"; break;
    default: week="星期天";
    }
    var str = "" + mydate.getFullYear() + " 年 " + (mydate.getMonth() + 1) + " 月 " + mydate.getDate() + " 日 " + week;
    $('.todaytime')[0].innerHTML = str;
    str = '"savesynctime" : "' + str + '"'
    return str;
  }
  console.log(syncTime());
</script>
</body>
</html>

示例二:

<script type="text/javascript">
    var date = new Date();
 
    document.writeln(date);
    //Thu Jan 08 2015 01:25:53 GMT+0800 (中国标准时间)
    document.writeln(Date.parse());
    //NaN
    document.writeln(Date.parse('6/10/2014'));
    //1402329600000 毫秒数
    document.writeln(Date.parse('Thu Jan 08 2015 01:25:53'));
    //1420651553000 毫秒数
    document.writeln(Date.UTC());
    //NaN
 
    //日期格式化方法
    document.writeln('<br/>');
    document.writeln(date.toDateString());
    //Thu Jan 08 2015
    document.writeln('<br/>')
    document.writeln(date.toTimeString());
    //01:39:08 GMT+0800 (中国标准时间)
    document.writeln('<br/>')
    document.writeln(date.toLocaleDateString());
    //2015年1月8日
    document.writeln('<br/>')
    document.writeln(date.toLocaleTimeString());
    //上午1:39:08
    document.writeln('<br/>')
    document.writeln(date.toUTCString());
    //Wed, 07 Jan 2015 17:39:08 GMT
 
    //一些set,get方法
 
 
  </script>

示例三:

Date.prototype.Format = function(formatStr) {
  var str = formatStr;
  var Week = ['日', '一', '二', '三', '四', '五', '六'];
  str = str.replace(/yyyy|YYYY/, this.getFullYear());
  str = str.replace(/yy|YY/, (this.getYear() % 100) > 9 ? (this.getYear() % 100).toString() : '0' + (this.getYear() % 100));
  str = str.replace(/MM/, (this.getMonth() + 1) > 9 ? (this.getMonth() + 1).toString() : '0' + (this.getMonth() + 1));
  str = str.replace(/M/g, (this.getMonth() + 1));
  str = str.replace(/w|W/g, Week[this.getDay()]);
  str = str.replace(/dd|DD/, this.getDate() > 9 ? this.getDate().toString() : '0' + this.getDate());
  str = str.replace(/d|D/g, this.getDate());
  str = str.replace(/hh|HH/, this.getHours() > 9 ? this.getHours().toString() : '0' + this.getHours());
  str = str.replace(/h|H/g, this.getHours());
  str = str.replace(/mm/, this.getMinutes() > 9 ? this.getMinutes().toString() : '0' + this.getMinutes());
  str = str.replace(/m/g, this.getMinutes());
  str = str.replace(/ss|SS/, this.getSeconds() > 9 ? this.getSeconds().toString() : '0' + this.getSeconds());
  str = str.replace(/s|S/g, this.getSeconds());
  return str
}

示例四:

Date.prototype.toString=function(format,loc){
  var time={};
  time.Year=this.getFullYear();
  time.TYear=(""+time.Year).substr(2);
  time.Month=this.getMonth()+1;
  time.TMonth=time.Month<10?"0"+time.Month:time.Month;
  time.Day=this.getDate();
  time.TDay=time.Day<10?"0"+time.Day:time.Day;
  time.Hour=this.getHours();
  time.THour=time.Hour<10?"0"+time.Hour:time.Hour;
  time.hour=time.Hour<13?time.Hour:time.Hour-12;
  time.Thour=time.hour<10?"0"+time.hour:time.hour;
  time.Minute=this.getMinutes();
  time.TMinute=time.Minute<10?"0"+time.Minute:time.Minute;
  time.Second=this.getSeconds();
  time.TSecond=time.Second<10?"0"+time.Second:time.Second;
  time.Millisecond=this.getMilliseconds();
  time.Week=this.getDay();
 
  var MMMArrEn=["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
  var MMMArr=["一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"];
  var WeekArrEn=["Sun","Mon","Tue","Web","Thu","Fri","Sat"];
  var WeekArr=["星期日","星期一","星期二","星期三","星期四","星期五","星期六"];
 
  var oNumber=time.Millisecond/1000;
 
  if(format!=undefined && format.replace(/\s/g,"").length>0){
    if(loc!=undefined && loc =="en"){
      MMMArr=MMMArrEn.slice(0);
      WeekArr=WeekArrEn.slice(0);
    }
    format=format
      .replace(/yyyy/ig,time.Year)
      .replace(/yyy/ig,time.Year)
      .replace(/yy/ig,time.TYear)
      .replace(/y/ig,time.TYear)
      .replace(/MMM/g,MMMArr[time.Month-1])
      .replace(/MM/g,time.TMonth)
      .replace(/M/g,time.Month)
      .replace(/dd/ig,time.TDay)
      .replace(/d/ig,time.Day)
      .replace(/HH/g,time.THour)
      .replace(/H/g,time.Hour)
      .replace(/hh/g,time.Thour)
      .replace(/h/g,time.hour)
      .replace(/mm/g,time.TMinute)
      .replace(/m/g,time.Minute)
      .replace(/ss/ig,time.TSecond)
      .replace(/s/ig,time.Second)
      .replace(/fff/ig,time.Millisecond)
      .replace(/ff/ig,oNumber.toFixed(2)*100)
      .replace(/f/ig,oNumber.toFixed(1)*10)
      .replace(/EEE/g,WeekArr[time.Week]);
  }
  else{
    format=time.Year+"-"+time.Month+"-"+time.Day+" "+time.Hour+":"+time.Minute+":"+time.Second;
  }
  return format;
}
 
var d=new Date();
console.log(d.toString());  //2014-7-27 9:26:52
console.log(d.toString(""));  //2014-7-27 9:26:52
console.log(d.toString("yyyy-MM-dd HH:mm:ss"));  //2014-07-27 09:26:52
console.log(d.toString("yyyy年MM月dd日 HH:mm:ss"));  //2014年07月27日 09:26:52
console.log(d.toString("yyyy-MM-dd HH:mm:ss fff"));  //2014-07-27 09:26:52 237
console.log(d.toString("yyyy年 MMM dd EEE"));  //2014年 七月 27 星期五
console.log(d.toString("yyyy MMM dd EEE","en"));  //2014 Jul 27 Fri

示例五:时间个性化输出功能

/*
1、< 60s, 显示为“刚刚”
2、>= 1min && < 60 min, 显示与当前时间差“XX分钟前”
3、>= 60min && < 1day, 显示与当前时间差“今天 XX:XX”
4、>= 1day && < 1year, 显示日期“XX月XX日 XX:XX”
5、>= 1year, 显示具体日期“XXXX年XX月XX日 XX:XX”
*/
function timeFormat(time){
  var date = new Date(time),
    curDate = new Date(),
    year = date.getFullYear(),
    month = date.getMonth() + 10,
    day = date.getDate(),
    hour = date.getHours(),
    minute = date.getMinutes(),
    curYear = curDate.getFullYear(),
    curHour = curDate.getHours(),
    timeStr;
 
  if(year < curYear){
    timeStr = year +'年'+ month +'月'+ day +'日 '+ hour +':'+ minute;
  }else{
    var pastTime = curDate - date,
      pastH = pastTime/3600000;
 
    if(pastH > curHour){
       timeStr = month +'月'+ day +'日 '+ hour +':'+ minute;
    }else if(pastH >= 1){
       timeStr = '今天 ' + hour +':'+ minute +'分';
    }else{
       var pastM = curDate.getMinutes() - minute;
       if(pastM > 1){
        timeStr = pastM +'分钟前';
       }else{
        timeStr = '刚刚';
       }
    }
  }
  return timeStr;
}

以上所述就是本文的全部内容了,希望大家能够喜欢。

Javascript 相关文章推荐
让JavaScript拥有类似Lambda表达式编程能力的方法
Sep 12 Javascript
关于event.cancelBubble和event.stopPropagation()的区别介绍
Dec 11 Javascript
javascript函数式编程程序员的工具集
Oct 11 Javascript
backbone简介_动力节点Java学院整理
Jul 14 Javascript
jQuery实现广告条滚动效果
Aug 22 jQuery
vue.js实现标签页切换效果
Jun 07 Javascript
微信小程序中button去除默认的边框实例代码
Aug 01 Javascript
vue父子组件通信的高级用法示例
Aug 29 Javascript
改变layer confirm弹窗按钮的颜色方法
Sep 12 Javascript
ES6函数和数组用法实例分析
May 23 Javascript
详解Webpack4多页应用打包方案
Jul 16 Javascript
动态实现element ui的el-table某列数据不同样式的示例
Jan 22 Javascript
JavaScript中数据结构与算法(四):串(BF)
Jun 19 #Javascript
JavaScript中数据结构与算法(三):链表
Jun 19 #Javascript
js结合正则实现国内手机号段校验
Jun 19 #Javascript
JavaScript中数据结构与算法(二):队列
Jun 19 #Javascript
JavaScript中数据结构与算法(一):栈
Jun 19 #Javascript
JQuery中模拟image的ajaxPrefilter与ajaxTransport处理
Jun 19 #Javascript
c#程序员对TypeScript的认识过程
Jun 19 #Javascript
You might like
smarty静态实验表明,网络上是错的~呵呵
2006/11/25 PHP
php radio 单选框获取与保持值的实现代码
2010/05/15 PHP
微信公众平台接口开发入门示例
2014/12/24 PHP
PHP+redis实现添加处理投票的方法
2015/11/14 PHP
PHP中CheckBox多选框上传失败的代码写法
2017/02/13 PHP
JS 密码强度验证(兼容IE,火狐,谷歌)
2010/03/15 Javascript
6款经典实用的jQuery小插件及源码(对话框/提示工具等等)
2013/02/04 Javascript
Extjs4 GridPanel 的几种样式使用介绍
2013/04/18 Javascript
JQuery的自定义事件代码,触发,绑定简单实例
2013/08/01 Javascript
$.get获取一个文件的内容示例代码
2013/09/11 Javascript
JavaScript关闭当前页面(窗口)不带任何提示
2014/03/26 Javascript
JS产生随机数的用法小结
2016/12/10 Javascript
ReactJS实现表单的单选多选和反选的示例
2017/10/13 Javascript
js实现rem自动匹配计算font-size的示例
2017/11/18 Javascript
jQuery实现获取动态添加的标签对象示例
2018/06/28 jQuery
Vue props用法详解(小结)
2018/07/03 Javascript
详解webpack打包nodejs项目(前端代码)
2018/09/19 NodeJs
nodejs dgram模块广播+组播的实现示例
2019/11/04 NodeJs
微信小程序开发(二):页面跳转并传参操作示例
2020/06/01 Javascript
JavaScript实现弹出窗口效果
2020/12/09 Javascript
python实现上传样本到virustotal并查询扫描信息的方法
2014/10/05 Python
用Python写冒泡排序代码
2016/04/12 Python
Python中内置的日志模块logging用法详解
2016/07/12 Python
Python enumerate索引迭代代码解析
2018/01/19 Python
详解Pytorch 使用Pytorch拟合多项式(多项式回归)
2018/05/24 Python
Python图片的横坐标汉字实例
2019/12/04 Python
python+django+selenium搭建简易自动化测试
2020/08/19 Python
Python importlib模块重载使用方法详解
2020/10/13 Python
加拿大消费电子和手机购物网站:The Source
2017/01/28 全球购物
英国蜡烛、蜡烛配件和家居香氛购买网站:Yankee Candle
2018/12/12 全球购物
幼儿教师师德师风自我剖析材料
2014/09/29 职场文书
毕业生见习报告总结
2014/11/08 职场文书
停电放假通知
2015/04/14 职场文书
工作态度不好检讨书
2015/05/06 职场文书
关于教师节的广播稿
2015/08/19 职场文书
员工给公司的建议书
2019/06/24 职场文书