js 格式化时间日期函数小结
下面是脚本之家为大家整理的一些格式化时间日期的函数代码,需要的朋友可以参考下。
代码如下:
Date.prototype.format = function(format){
var o = {
"M+" : this.getMonth()+1, //month
"d+" : this.getDate(), //day
"h+" : this.getHours(), //hour
"m+" : this.getMinutes(), //minute
"s+" : this.getSeconds(), //second
"q+" : Math.floor((this.getMonth()+3)/3), //quarter
"S" : this.getMilliseconds() //millisecond
}
if(/(y+)/.test(format)) {
format = format.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
}
for(var k in o) {
if(new RegExp("("+ k +")").test(format)) {
format = format.replace(RegExp.$1, RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
}
}
return format;
}
//使用方法
var now = new Date();
var nowStr = now.format("yyyy-MM-dd hh:mm:ss");
//使用方法2:
var testDate = new Date();
var testStr = testDate.format("YYYY年MM月dd日hh小时mm分ss秒");
alert(testStr);
//示例:
alert(new Date().Format("yyyy年MM月dd日"));
alert(new Date().Format("MM/dd/yyyy"));
alert(new Date().Format("yyyyMMdd"));
alert(new Date().Format("yyyy-MM-dd hh:mm:ss"));
js格式化当前时间为yyyy-mm-dd形式
function getNowFormatDate()
{
var day = new Date();
var Year = 0;
var Month = 0;
var Day = 0;
var CurrentDate = "";
//初始化时间
//Year= day.getYear();//有火狐下2008年显示108的bug
Year= day.getFullYear();//ie火狐下都可以
Month= day.getMonth()+1;
Day = day.getDate();
//Hour = day.getHours();
// Minute = day.getMinutes();
// Second = day.getSeconds();
CurrentDate += Year + "-";
if (Month >= 10 )
{
CurrentDate += Month + "-";
}
else
{
CurrentDate += "0" + Month + "-";
}
if (Day >= 10 )
{
CurrentDate += Day ;
}
else
{
CurrentDate += "0" + Day ;
}
return CurrentDate;
}
js 格式化时间日期函数小结的更多相关文章
- js 格式化时间日期函数小结3
function DateUtil(){}/***功能:格式化时间*示例:DateUtil.Format("yyyy/MM/dd","Thu Nov 9 20:30:37 ...
- js 格式化时间日期函数小结2
方法一: // 对Date的扩展,将 Date 转化为指定格式的String // 月(M).日(d).小时(h).分(m).秒(s).季度(q) 可以用 1-2 个占位符, // 年(y)可以用 ...
- mysql 常用的时间日期函数小结
本文主要是总结一些常用的在实际运用中常用的一些mysql时间日期以及转换的函数 1.now() :返回当前日期和时间 select now(); //2018-04-21 09:19:21 2.cu ...
- js 时间日期函数小结
Date.prototype.format = function(format){ var o = { "M+" : this.getMonth()+1, //month &quo ...
- js 格式化时间日期
Date.prototype.format = function(format){ var o = { "M+" : this.getMonth()+1, //month &quo ...
- JS格式化时间并比较
JS格式化时间,然后进行比较.工作遇到的情况,然后网上找到的,记下来,下次用! </head> <body> <button onclick="myFuncti ...
- SQLite中的时间日期函数(转)
SQLite包含了如下时间/日期函数: datetime().......................产生日期和时间date()...........................产生日期tim ...
- SQLite中的时间日期函数
SQLite包含了如下时间/日期函数: datetime().......................产生日期和时间 date()...........................产生日期 t ...
- js格式化时间的方法
方法一:用js格式化时间的方法. Date.prototype.format =function(format) { var o = { "M+" : this.getMonth( ...
随机推荐
- TJU Problem 2101 Bullseye
注意代码中: result1 << " to " << result2 << ", PLAYER 1 WINS."<& ...
- Spring mvc 导出table到Excel
/** * * @Title: exportExcel * @Description: TODO(导出到excel) * @param Page page * @return ModelAndView ...
- 2017.7.11 fuse工作原理
FUSE的工作原理如图所示.假设基于FUSE的用户态文件系统hello挂载在/tmp/fuse目录下.当应用层程序要访问/tmp/fuse下的文件时,通过glibc中的函数进行系统调用,处理这些系统调 ...
- 杜教BM【转载】
https://blog.csdn.net/qq_36876305/article/details/80275708 #include <bits/stdc++.h> using name ...
- 《DSP using MATLAB》Problem 5.34
第1小题 代码: %% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ...
- springMvc前后台传值各种情况
本文转载至:https://blog.csdn.net/pangliang_csdn/article/details/52486787 以后自述...
- 【mybatis源码学习】利用maven插件自动生成mybatis代码
[一]在要生成代码的项目模块的pom.xml文件中添加maven插件 <!--mybatis代码生成器--> <plugin> <groupId>org.mybat ...
- day39机器学习
2 Numpy快速上手 2.1. 什么是Numpy Numpy是Python的一个科学计算的库 主要提供矩阵运算的功能,而矩阵运算在机器学习领域应用非常广泛 Numpy一般与Scipy.matplot ...
- java黑魔法-反射机制-02-通过Java反射调用其他类方法
package com.aaron.reflect; import java.lang.reflect.Method; import java.lang.reflect.InvocationTarge ...
- pnpm 快速节省磁盘工具的包管理工具
nodejs 相关的包管理工具有很多,我们常用的有 npm cnpm(我基本已经不用了),yarn... pnpm 是另外一个不错的包管理工具,包含以下特性 快速 节省空间,一个版本的包只会在磁盘中存 ...