将new Date() 格式化为 ’2018-10-11‘ 的字符串格式
function dateToString( date , format ){
if(!date) return "";
if (!Common.type.isDate(date)) return ""; //详见博客:常用类型判断
format = format ? format : "y-m-d";
switch(format){
case "y-m":
return date.getFullYear() + "-" + datePad( date.getMonth() + 1, 2 );
case "y-m-d":
return date.getFullYear() + "-" + datePad( date.getMonth() + 1, 2 ) + "-" + datePad( date.getDate(), 2 );
case "h-m-s":
return datePad( date.getHours(), 2 ) + ":" + datePad( date.getMinutes(), 2 ) + ":" + datePad( date.getSeconds(), 2);
case "y-m-d-h-m-s":
return date.getFullYear() + "-" + datePad( date.getMonth() + 1, 2 ) + "-" + datePad( date.getDate(), 2 ) + " " + datePad( date.getHours(), 2 ) + ":" + datePad( date.getMinutes(), 2 ) + ":" + datePad( date.getSeconds(), 2);
}
}
function datePad(num, n){
if( ( num + "" ).length >= n )
return num; //一位数
return arguments.callee( "0" + num, n ); //两位数
}
Common.type.isDate: function() {
for (var b = 0, a = arguments.length; b < a; b++) {
o = arguments[b];
if (!(System.type.isObject(o) && o.constructor && (o.constructor.toString().indexOf("Date") > -1 || o instanceof Date))) {
return false;
}
}
return true;
}
例:
var today = new Date();
dateToString(today); // '2017-10-11'
dateToString(today,'y-m'); //'2017-10'
dateToString(today,'y-m-d-h-m-s'); //'2017-10-11 16:42:59'
将new Date() 格式化为 ’2018-10-11‘ 的字符串格式的更多相关文章
- 把json格式对象转成可提交字符串格式,会过滤掉函数 {a: {b: 3}, b: [1], c: "d"} -> a.b=3&b[0]=1&c=d
var json = { name: "任务名称" , scoreRule: "", score: "", // 如果规则表达式不为空,则默 ...
- 转 sql 时间转换格式 convert(varchar(10),字段名,转换格式)
convert(varchar(10),字段名,转换格式) CONVERT(nvarchar(10),count_time,121)CONVERT为日期转换函数,一般就是在时间类型(datetime, ...
- (转)sql 时间转换格式 convert(varchar(10),字段名,转换格式)
convert(varchar(10),字段名,转换格式) CONVERT(nvarchar(10),count_time,121) CONVERT为日期转换函数,一般就是在时间类型(datetime ...
- Sybase datetime 时间转换格式 convert(varchar(10),字段名,转换格式)
convert(varchar(10),字段名,转换格式)sybase下convert函数第三个参数(时间格式)比如:1.select user_id,convert(varchar(10),dayt ...
- sql 时间转换格式 convert(varchar(10),字段名,转换格式)
convert(varchar(10),字段名,转换格式) CONVERT(nvarchar(10),count_time,121) CONVERT为日期转换函数,一般就是在时间类型(datetime ...
- 【2018.10.11 C与C++基础】C Preprocessor的功能及缺陷(草稿)
一.前言及参考资料 C Preprocessor即所谓的C预处理器,C++也继承了C的预处理程序,但在C++语言的设计与演化一书中,C++的设计者Bjarne Strustrup提及他从未喜欢过C预处 ...
- convert(varchar(10),字段名,转换格式
sql 时间转换格式 ) convert(varchar(10),字段名,转换格式) CONVERT(nvarchar(10),count_time,121)CONVERT为日期转换函数,一般就是在时 ...
- toJSON() 方法,将 Date 对象转换为字符串,并格式化为 JSON 数据格式。
JavaScript toJSON() 方法 定义和用法 toJSON() 方法可以将 Date 对象转换为字符串,并格式化为 JSON 数据格式. JSON 数据用同样的格式就像x ISO-8601 ...
- 【转】使用DateFormat把时间长度格式化为"时:分:秒"格式--不错
原文网址:http://ssd910.blog.163.com/blog/static/238767972010112214114201/ 经常在系统中显示时间长度,基本上每次都是显示秒数,客户觉得按 ...
- Javascript将字符串日期格式化为yyyy-mm-dd的方法 js number 类型 没有length 属性 string类型才有
日期格式化相信对于大家来说再熟悉不过,最近工作中自己利用Javascript就写了一个,现在将实现的代码分享给大家,希望对有需要的朋友们能有所帮助,感兴趣的朋友们下面来一起看看吧. 这篇文章主要介绍的 ...
随机推荐
- 将Python文件打包为exe文件,并在控制台运行之简易教程
第一步 在线安装 pyinstaller. 方法:打开win+ R,输入cmd,在命令行输入"pip install pyinstaller" 静等几分钟后即可安装成功. ...
- 希尔排序(java实现)
上篇blog中介绍的直接插入排序,希尔排序就是对直接插入排序的一个优化.比如有这么一种情况:对一个无序数组进行从小到大的排序,但是数组的最后一个位置的数是最小的,我们要把它挪到第一个位置,其他位置的都 ...
- Java 常用类String类、StringBuffer类
常用类 String类.StringBuffer类 String代表不可变的字符序列 "xxxxxxx"为该类的对象 举例(1) public class Test { publi ...
- 福大软工1816:Beta(7/7)
Beta 冲刺 (7/7) 队名:第三视角 组长博客链接 本次作业链接 团队部分 团队燃尽图 工作情况汇报 张扬(组长) 过去两天完成了哪些任务 文字/口头描述 组织会议 wxpy中多个功能的开发 整 ...
- 第5章 Linux 常用网络指令
网络参数设定使用的指令 手动/自动设定与启动/关闭 IP 参数: ifconfig, ifup, ifdown ifconfig :查询.设定网络卡与 IP 网域等相关参数:ifup, ifdown: ...
- purcell的emacs配置中的自动补全功能开启
标记一下,原文参看purcell的emacs配置中的自动补全功能开启 修改init-auto-complete.el文件 ;;(setq-default ac-expand-on-auto-compl ...
- [ Selenium2 从零开始 by Bruce from http://seleniumcn.cn ] 1-8 视频集锦
内容转自: http://blog.csdn.net/sxl0727tu/article/details/51887093\ ************************************* ...
- 设计模式PHP篇(三)————装饰器模式
简单的用php实现了装饰器模式: <?php /** *简单的装饰器模式 */ class PrintText { protected $decorators = []; public func ...
- 更改HTTP头信息
http信息分三部分 1.请求行 GET lizi.php HTTP/1.1 2.HTTP头信 Host: localhost Connection: keep-alive Cache-Contr ...
- c 结构体读取与保存
1.结构体保存到文本 #include <stdio.h> #include <stdlib.h> #include <string.h> #define max ...