// 对Date的扩展,将 Date 转化为指定格式的String
// 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符,
// 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
// 例子:
// (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
// (new Date()).Format("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18 Date.prototype.Format = function(fmt){
var o = {
"M+": this.getMonth()+1,
"d+": this.getDate(),
"H+": this.getHours(),
"m+": this.getMinutes(),
"s+": this.getSeconds(),
"S+": this.getMilliseconds()
}; //因位date.getFullYear()出来的结果是number类型的,所以为了让结果变成字符串型,下面有两种方法: if(/(y+)/.test(fmt)){
//第一种:利用字符串连接符“+”给date.getFullYear()+"",加一个空字符串便可以将number类型转换成字符串。 fmt=fmt.replace(RegExp.$1,(this.getFullYear()+"").substr(4-RegExp.$1.length));
}
for(var k in o){
if (new RegExp("(" + k +")").test(fmt)){ //第二种:使用String()类型进行强制数据类型转换String(date.getFullYear()),这种更容易理解。 fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(String(o[k]).length)));
}
}
return fmt;
}
window.onload=function(){
var time1 = new Date().Format("yyyy-MM-dd");
var time2 = new Date().Format("yyyy-MM-dd HH:mm:ss");
alert(time1);
alert(time2); };

 格式化时间

    // 格式化日期函数
function formatDate(v) {
if (/^(-)?\d{1,10}$/.test(v)) {
v = v * 1000;
} else if (/^(-)?\d{1,13}$/.test(v)) {
v = v * 1;
}
var dateObj = new Date(v);
var month = dateObj.getMonth() + 1;
var day = dateObj.getDate();
var hours = dateObj.getHours();
var minutes = dateObj.getMinutes();
var seconds = dateObj.getSeconds();
if(month<10){
month = "0" + month;
}
if(day<10){
day = "0" + day;
}
if(hours<10){
hours = "0" + hours;
}
if(minutes<10){
minutes = "0" + minutes;
}
if(seconds<10){
seconds = "0" + seconds;
}
var UnixTimeToDate = dateObj.getFullYear() + '-' + month + '-' +day + ' ' + hours + ':' + minutes + ':' + seconds;
return UnixTimeToDate;
}

JS关于Date函数的格式化输出的更多相关文章

  1. python的print函数的格式化输出

    使用print函数的时候,可以像C一样格式化输出,同时还支持参数化输出 print('%s' % ("CooMark")) print('整数|%d|' % (123)) prin ...

  2. Linux下date命令,格式化输出,时间设置

    date命令的帮助信息 [root@localhost source]# date --help用法:date [选项]... [+格式] 或:date [-u|--utc|--universal] ...

  3. go语言中的strings常用函数和格式化输出

    package main; import ( "fmt" "strings" ) type person struct { name string; age i ...

  4. awk内置字符串函数 awk 格式化输出

    i249 ~ # ps -efl|head -1|awk '$2~/S/{print $2}'Si249 ~ # ps -efl|awk '$2~/S/{print $2}'SSSS printf - ...

  5. 【心得】怪异的JS的Date函数

    我们知道new Date('2013/1/1')是2013年1月1日, 那么new Date('2013/1/366')会报无效日期格式吗?答案是,这是一个有效的日期,但是他是表示2014年1月1日. ...

  6. 8.Java格式化输出

    JAVA中字符串输出格式 1.使用format函数 System.out.format("%d  %f",10,10.5); 2.使用Formatter类 构造函数Formatte ...

  7. 『Go基础』第8节 格式化输出

    输出就是将数据信息打印到电脑屏幕上. 本节我们就来学习一下Go语言中的三种输出方式: Print().Println().Printf(). 1.Print() Print()主要的一个特点就是打印数 ...

  8. js Date()获取时间,格式化输出,时间比较大小

    1.获取时间并且格式化输出 new Date().toLocaleString('cn',{hour12:false}) //2018/12/6 17:57:15 new Date().toLocal ...

  9. 『开源』扩展 JS 的 Date 处理函数

    背景: JS 有自己的 时间类型 Date  —— 但是,在某些情况下 这个对象似乎 不太好用. 本文 基于 JQuery 扩展了一些  JS日期函数,包括: > 字符串 转 Date 对象 万 ...

随机推荐

  1. git 服务器 LINUX端的使用

    首先,需要建立一个git服务器----- 这里介绍如何使用git这个服务器 我们在github上下载一份代码,里面有如下内容 我们使用git服务器的时候不能有.git 文件,所以在此将其删除 ys-l ...

  2. C# 关于X86/X64/AnyCpu 的关系

    电脑硬件CPU可以分为x86与x64, x86的机器只能安装32位的操作系统,如XP, WIN7_86, x64的机器既可以安装32位的系统,又可以安装64位的系统,只是在x64的机器上安装32位的系 ...

  3. 出错:Error creating bean with name 'studentServiceImpl': Unsatisfied dependency expressed through field 'studentMapper';

    详细错误: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with nam ...

  4. Centos7创建支持ssh服务的docker镜像

    如何在centos7中使用docker创建一个支持ssh连接的容器 1.拉取centos7.4镜像(由于7.4目前是最稳定的版本,所以推荐使用centos7.4) docker pull centos ...

  5. 使用Jenkins时,如果GIT_COMMIT无变化,跳过构建

    使用Jenkins时,如果GIT_COMMIT无变化,跳过构建    使用插件: conditional-buildstep A buildstep wrapping any number of ot ...

  6. vmware 虚拟机报错 删除文件夹,可以恢复

  7. SSM框架:解决后台传数据到前台中文乱码问题,使用@ResponseBody返回json 中文乱码

    解决方法一:@RequestMapping(value="/getphone",produces = "text/plain;charset=utf-8") / ...

  8. C# 返回JSON格式化统一标准

    public class BackJson { public int code { get; set; } public string msg { get; set; } public string ...

  9. iOS 使用GRMustache对HTML页面进行渲染

    第三方控件[GRMustanche] OC Swift //测试版本 7.3.2 最终效果图: [使用方法&& 核心代码] 通过cocoapods 导入 #import <GRM ...

  10. Centos6.5系统关闭防火墙

    关闭Centos6.5系统防火墙步骤: 1.命令:service iptables stop //停止正在运行的防火墙服务 2.命令:chkconfig iptables off //永久关闭防火墙 ...