js 格式化时间日期函数小结
来源:互联网 发布:淘宝客服网上兼职 编辑:程序博客网 时间:2024/04/28 12:45
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"));
通过字符串截取格式化时间
function fn_getdate(datetime) {
var newdate = "";
if (datetime != null && datetime != '') {
//newdate = datetime.substring(0, 4);
//newdate += "";
newdate += datetime.substring(5, 7);
newdate += "月";
newdate += datetime.substring(8, 10);
newdate += "日";
}
return newdate;
}
function fn_gettime(datetime) {
var newdate = "";
if (datetime != null && datetime != '') {
newdate += datetime.substring(11, 13);
newdate += ":";
newdate += datetime.substring(14, 16);
}
return newdate;
//时间转换
function fn_getdatetwo(datetime) {
var newdate = "";
if (datetime != null && datetime != '') {
newdate = datetime.substring(0, 4);
newdate += "-";
newdate += datetime.substring(5, 7);
newdate += "-";
newdate += datetime.substring(8, 10);
//newdate += "日";
}
return newdate;
}
用法fn_getdatetwo(new Date())
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;
}
0 0
- js 格式化时间日期函数小结
- js 格式化时间日期函数小结
- js 格式化时间日期函数小结
- js 格式化时间日期函数小结
- js格式化时间日期函数
- js 格式化时间日期函数
- js日期时间格式化函数
- js格式化日期/时间函数
- js 时间戳 日期格式化 函数
- js时间日期格式化
- js日期时间格式化
- js 日期时间格式化
- js 格式化日期时间
- 格式化时间日期函数
- javascript时间格式format函数,js日期格式化函数
- js日期格式化函数
- js日期格式化函数
- js 日期格式化函数
- 死锁语法
- C++通过Callback向C#传递数据
- shell总结
- android捕获异常并保存到sd卡文件目录里
- 使用 VisualVM 进行性能分析及调优
- js 格式化时间日期函数小结
- facebook share 功能demo
- win32项目--获取、修改计算机屏幕分辨率
- IntelliJ IDEA 开发scala
- pdf转换成html转换工具的使用方法
- android 实现拍照的2种方法
- VS2010使用EF6连接mysql 配置
- 基于 Jenkins 快速搭建持续集成环境
- megapix-image.js使用的一个坑-ios图片裁剪之画布绘制大图片