就是一个工具类  开箱即用

传进一个指定的参数,格式化

//将时间戳格式化
function getMyDate(time){
if(typeof(time)=="undefined"){
return "";
}
var oDate = new Date(time),
oYear = oDate.getFullYear(),
oMonth = oDate.getMonth()+1,
oDay = oDate.getDate(),
oHour = oDate.getHours(),
oMin = oDate.getMinutes(),
oSen = oDate.getSeconds(),
oTime = oYear +'-'+ getzf(oMonth) +'-'+ getzf(oDay) +' '+ getzf(oHour) +':'+ getzf(oMin) +':'+getzf(oSen);//最后拼接时间 return oTime;
}; //补0操作,当时间数据小于10的时候,给该数据前面加一个0
function getzf(num){
if(parseInt(num) < 10){
num = '0'+num;
}
return num;
}

获取当前的日期时间 格式“yyyy-MM-dd HH:MM:SS”

function getNowFormatDate() {
var date = new Date();
var seperator1 = "-";
var seperator2 = ":";
var month = date.getMonth() + 1;
var strDate = date.getDate();
if (month >= 1 && month <= 9) {
month = "0" + month;
}
if (strDate >= 0 && strDate <= 9) {
strDate = "0" + strDate;
}
var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
+ " " + date.getHours() + seperator2 + date.getMinutes()
+ seperator2 + date.getSeconds();
return currentdate;
}

获取当前时间,格式YYYY-MM-DD

 function getNowFormatDate() {
var date = new Date();
var seperator1 = "-";
var year = date.getFullYear();
var month = date.getMonth() + 1;
var strDate = date.getDate();
if (month >= 1 && month <= 9) {
month = "0" + month;
}
if (strDate >= 0 && strDate <= 9) {
strDate = "0" + strDate;
}
var currentdate = year + seperator1 + month + seperator1 + strDate;
return currentdate;
}

获取当前的星期几

//一行啊~~!TMD居然一样代码就写了
var str = "今天是星期" + "日一二三四五六".charAt(new Date().getDay());
console.log(str);

给出一个日期,计算与当前时间还有多少天

//计算日期相减天数
function DateMinus(time){
var sdate = new Date(time);
var now = new Date();
var days = now.getTime() - sdate.getTime();
var day = parseInt(days / (1000 * 60 * 60 * 24));
return day;
}
let number = DateMinus();
console.log(number) time 只要是符合时间格式的字符串就行。比如:
var sdate = new Date("Sep 22, 2018 12:00:00 AM");
var sdate = new Date("2018-8-09");

最新文章

  1. 用Python写一个简单的Web框架
  2. Mahout安装与配置
  3. ecshop /includes/lib_base.php、/includes/fckeditor/editor/dialog/fck_spellerpages/spellerpages/server-scripts/spellchecker.php Backdoor Vul
  4. VMP虚拟机加壳的原理学习
  5. c#扩展方法-摘自msdn
  6. PAT 1070. Mooncake (25)
  7. (转)25个增强iOS应用程序性能的提示和技巧--中级篇
  8. Solr4.2迁移到新项目下异常:java.lang.NoSuchMethodError: org.apache.http.conn.scheme.Scheme.&lt;init&gt;
  9. GitHub 简单用法
  10. InstallShield卸载状态
  11. CCF 高速公路 tarjan求强连通分量
  12. [转载]Require.js Example – Setup Time 2 Minutes
  13. 【转载】VC操作剪切板
  14. PL/sql中如何声明变量,常量,控制语句及for,loop,while和顺序控制的使用
  15. android MotionEvent
  16. 如何在Windows 10 IoT Core中添加其他语言的支持,如中文
  17. mq_学习_00_资源帖
  18. APIO2018 题解
  19. 员工管理系统(集合与IO流的结合使用 beta4.0 ObjectInputStream/ ObjectOutputStream)
  20. Flume安装部署

热门文章

  1. QT应用在windows和Linux平台的发布指南
  2. CMake系列之三:多个源文件-同一目录
  3. eclipse新建maven项目,修改默认jdk版本
  4. mac安装sublime text 3,含注册码
  5. Linux&amp;docker&amp;cgroups
  6. Chrome 启动参数列表
  7. Axios插件和loading的实现
  8. laravel 多个项目共享SESSION
  9. Node buffer模块缓冲区
  10. poj 3694 Network(割边+lca)