Js 时间与字符串转示例
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Js 时间与字符串转示例</title>
<script type="text/javascript">
function formatDate(date, format) {
if (!date) return;
if (!format) format = "yyyy-MM-dd";
switch (typeof date) {
case "string":
date = new Date(date.replace(/-/, "/"));
break;
case "number":
date = new Date(date);
break;
}
if (!date instanceof Date) return;
var dict = {
"yyyy": date.getFullYear(),
"M": date.getMonth() + 1,
"d": date.getDate(),
"H": date.getHours(),
"m": date.getMinutes(),
"s": date.getSeconds(),
"MM": ("" + (date.getMonth() + 101)).substr(1),
"dd": ("" + (date.getDate() + 100)).substr(1),
"HH": ("" + (date.getHours() + 100)).substr(1),
"mm": ("" + (date.getMinutes() + 100)).substr(1),
"ss": ("" + (date.getSeconds() + 100)).substr(1)
};
return format.replace(/(yyyy|MM?|dd?|HH?|ss?|mm?)/g, function () {
return dict[arguments[0]];
});
} Date.prototype.Format = function (fmt) { //author: meizz
var o = {
"M+": this.getMonth() + 1, //月份
"d+": this.getDate(), //日
"h+": this.getHours(), //小时
"m+": this.getMinutes(), //分
"s+": this.getSeconds(), //秒
"q+": Math.floor((this.getMonth() + 3) / 3), //季度
"S": this.getMilliseconds() //毫秒
};
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
return fmt;
}
/*-----日期转字符串----start-----------*/
var time2 = new Date().Format("yyyy-MM-dd hh:mm:ss");
alert(time2);
/*-----日期转字符串----end-----------*/ /*-----字符串转日期----start-----------*/
var str = '2012-08-12 23:13:15';
str = str.replace(/-/g, "/");
var date = new Date(str);
alert(date.getFullYear());
/*-----字符串转日期----end-----------*/
</script>
</head>
<body> </body>
</html>
Js 时间与字符串转示例的更多相关文章
- js 时间转字符串,转成yyyy-MM-dd HH:mm:SS格式
// 时间转字符串,转成yyyy-MM-dd HH:mm:SS格式 function dateToStr(datetime){ var dateTime = new Date(datetime); v ...
- JS时间和字符串的相互转换 Date+String
1.js字符串转换成时间 1.1方法一:输入的时间格式为yyyy-MM-dd function convertDateFromString(dateString) { if (dateString) ...
- js时间 字符串相互转化
js的时间和字符串的转化的讲解是有很多文章的,基本的都是一致的原理.不过曾经碰到过一个比较坑爹的需求,看到网上很少有相关的总结,所以自己简单的记录一下,给后来的同学们点思路. 当时的需求是这样子的,某 ...
- 表单序列化json字符串和js时间格式化
js时间格式化 new Date().format("时间格式") Date.prototype.format = function(fmt) { var o = { ...
- js截取中英文字符串、标点符号无乱码示例解读
<script> function subString(str, len, hasDot) { var newLength = 0; var newStr = ""; ...
- js 时间处理
1.格式化时间 function GetDateTimeFormatter(value) { if (value == undefined) { return &q ...
- js获取当前时间,js时间函数
Js获取当前日期时间及其它操作,js时间函数 var myDate = new Date(); myDate.getYear(); //获取当前年份(2位) myDate.getFullYear(); ...
- js 时间函数 及相关运算大全
js 时间函数 及相关运算大全 var myDate = new Date(); myDate.getYear(); //获取当前年份(2位) myDate.getFullYear(); ...
- Java开发笔记(四十四)本地日期时间与字符串的互相转换
之前介绍Calendar的时候,提到日历实例无法直接输出格式化后的时间字符串,必须先把Calendar类型转换成Date类型,再通过格式化工具SimpleDateFormat获得字符串.而日期时间的格 ...
随机推荐
- 【转】POP3、SMTP和IMAP之间的区别和联系
POP3 POP3是Post Office Protocol 3的简称,即邮局协议的第3个版本,它规定怎样将个人计算机连接到Internet的邮件服务器和下载电子邮件的电子协议.它是因特网电子邮件的第 ...
- sqlServer 存储过程执行遇到的问题及解决方案
1.EXEC 执行Sql语句被截断的问题: Sql语句: SET @sqlSel='SELECT '+@sqlField+', SUM(ISNULL(b.customsTariff_Sup,0))AS ...
- Show a heart shaped
Windows Form application version: private void Form1_Load(object sender, EventArgs e) { ...
- 使用 jQuery UI Widget Factory 编写有状态的插件(Stateful Plugins)
使用 jQuery UI Widget Factory 编写有状态的插件(Stateful Plugins) Note 这一章节的内容是基于 Scott Gonzalez 一篇博客 Building ...
- 深入理解HashMap
转自:http://annegu.iteye.com/blog/539465 Hashmap是一种非常常用的.应用广泛的数据类型,最近研究到相关的内容,就正好复习一下.网上关于hashmap的文章很多 ...
- [mysql] MariaDB 10.0.10 GTID复制
一:概念理解: 1.TID:Transaction ID,即Mysql服务器的事务ID号. 2.GTID:Global Transaction ID,全局事务ID,在整个主从复制架构中任何两个事 ...
- SharePoint 2010 最佳实践学习总结------第2章 SharePoint Windows PowerShell指南
第2章 SharePoint Windows PowerShell指南 SharePoint 2010是SharePoint系列产品中第一个开始支持Windows PowerShell的产品,在以前的 ...
- [转]StringUtils方法
摘自http://blog.sina.com.cn/s/blog_4550f3ca0100qrsd.html org.apache.commons.lang.StringUtils中方法的操作对象是j ...
- 04文件与IO
文件系统调用: open.close.create.read.write open: int open(const char* path, int flags) path:要打开的文件的路径 flag ...
- LeetCode "Binary Tree Vertical Order"
BFS + HashTable class Solution { int maxl, minl; unordered_map<int, vector<int>> hm; pub ...