这种效果如何实现? 在JavaScript中,会不会有像在msSQL那样格式花时间表达的方法存在? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 JS区分大小写你里面一个是mm一个是MM to benjaminwu198818:js里没mssql那么方便to chinmo :我上面有作大小写处理,我这些代码没有错,只是想问一下,如何实现这样形式的写法:alert( myDate("2009-03-26 12:30:00").toString("yyyy-mm-dd") ) http://blog.csdn.net/yzy0612/archive/2007/08/07/1730732.aspx自己看别人的吧看别人写的格式化 var myDate = function(d){ myDate._d=new Date(d.replace(/-/g, "/")); return myDate}; //new Object();myDate.toString = function( format ){ if(format && typeof format == "string") format = format.toLowerCase(); switch(format) { case "yyyy-mm-dd hh:mm:ss" : return "2009-03-26 12:00:00" case "yyyy-mm-dd hh:mm" : return "2009-03-26 12:00" case "yyyy-mm-dd" : return "2009-03-26" case "yyyy" : return "2009" case "mm" : return myDate._d.getMonth() + 1; default : return "2009-03-26 12:00:00" }}alert(myDate("2009-03-12").toString("mm")) 嗯,传递不了,重写Date对象的toString()方法不知道楼主的代码不要new object()而用new date()行不行? var myDate = function(d){ d=new Date(d.replace(/-/g, "/")); var f={}; f.toString = function( format ){ if(format && typeof format == "string") format = format.toLowerCase(); switch(format) { case "yyyy-mm-dd hh:mm:ss" : return "2009-03-26 12:00:00" case "yyyy-mm-dd hh:mm" : return "2009-03-26 12:00" case "yyyy-mm-dd" : return "2009-03-26" case "yyyy" : return "2009" case "mm" : return d.getMonth() + 1; default : return "2009-03-26 12:00:00" } } return f;}; //new Object();alert(myDate("2009-03-12").toString("mm"))这就能解决 学习 http://blog.csdn.net/yzy0612/archive/2007/08/07/1730732.aspx to silentwins : 扩展系统的Date,我开始有想到过,后来感觉还是另外再写。to chinmo : 您给的别人格式化那种方式,我见过,是一种不错的思路。to cloudgamer : 第一种方法感觉不错 return myDate后不需要用prototype定义function 单例有什么不好吗??????? (显然单例模式的要点有三个;一是某各类只能有一个实例;二是它必须自行创建这个事例;三是它必须自行向整个系统提供这个实例。) 您的第二种方法也可以实现我说的效果,但如果以后要继续扩展属性,方法,感觉就没办法了???? to cloudgamer :哦,明白,2种方式都可以加扩展,如:var myDate = function(d){ myDate._d=new Date(d.replace(/-/g, "/")); return myDate}; //new Object();myDate.toString = function( format ){ if(format && typeof format == "string") format = format.toLowerCase(); switch(format) { case "yyyy-mm-dd hh:mm:ss" : return "2009-03-26 12:00:00" case "yyyy-mm-dd hh:mm" : return "2009-03-26 12:00" case "yyyy-mm-dd" : return "2009-03-26" case "yyyy" : return "2009" case "mm" : return myDate._d.getMonth() + 1; default : return "2009-03-26 12:00:00" }}//新加的myDate.fun=function(){ return myDate._d;}alert(myDate("2009-03-12").toString("mm"))alert(myDate("2009-03-12").fun())var myDate = function(d){ d=new Date(d.replace(/-/g, "/")); var f={}; f.toString = function( format ){ if(format && typeof format == "string") format = format.toLowerCase(); switch(format) { case "yyyy-mm-dd hh:mm:ss" : return "2009-03-26 12:00:00" case "yyyy-mm-dd hh:mm" : return "2009-03-26 12:00" case "yyyy-mm-dd" : return "2009-03-26" case "yyyy" : return "2009" case "mm" : return d.getMonth() + 1; default : return "2009-03-26 12:00:00" } } //新加的 f.fun = function(){ return d } return f;}; //new Object();alert(myDate("2009-03-12").toString("mm"))alert(myDate("2009-03-12").fun()) 但这样的问题是只能单例 ---------------------------- 按你的意思是这儿用单例不好,为什么不好呢,麻烦你讲解一下?? 单例这里会出问题的var t1 = myDate("2009-03-12");var t2 = myDate("2009-04-12");alert(t1.toString());呵呵,cloudgamer的代码很好的解决了楼主的问题,不过每都是一个新的toStringfunction太浪费了我也来贴一种实现方式,<script type="text/javascript">(function(){ var myDate = window.myDate = function(d){ return new fn.init(d); }; //new Object(); var fn = myDate.prototype = { init: function(d){ this.date = new Date(d.replace(/-/g, "/")); }, toString: function( format ){ if(format && typeof format == "string") format = format.toLowerCase(); switch(format) { case "yyyy-mm-dd hh:mm:ss" : return "2009-03-26 12:00:00" case "yyyy-mm-dd hh:mm" : return "2009-03-26 12:00" case "yyyy-mm-dd" : return "2009-03-26" case "yyyy" : return "2009" case "mm" : return this.date.getMonth() + 1; default : return "2009-03-26 12:00:00" } }, valueOf:function(){ return this.date.valueOf(); } } fn.init.prototype = fn;})();var myDate1 = new myDate("2009-03-12"); //新建对象的方式1 var myDate2 = myDate("2009-04-12"); //新建对象的方式2 alert(myDate1.toString("mm"));alert(myDate2.valueOf());</script> 谢谢hzrui :你代码里的myDate.prototype = 是不是可以不要,我去掉显示结果一样,不知道你的用意fn.init.prototype = fn;这句不明白意思? myDate.prototype是可以不要fn.init.prototype = fn; fn是一个objfn.init是一个function, function.prototype = obj 仅此而已 用new也行var myDate = function(d){ return new f(d);}; //new Object();function f(d){ this._d=new Date(d.replace(/-/g, "/"));}f.prototype.toString = function( format ){ if(format && typeof format == "string") format = format.toLowerCase(); switch(format) { case "yyyy-mm-dd hh:mm:ss" : return "2009-03-26 12:00:00" case "yyyy-mm-dd hh:mm" : return "2009-03-26 12:00" case "yyyy-mm-dd" : return "2009-03-26" case "yyyy" : return "2009" case "mm" : return this._d.getMonth() + 1; default : return "2009-03-26 12:00:00" }}alert(myDate("2009-03-12").toString("mm")) 那实质上就是lz的方法2了只是把new放到函数里面 这样更好var myDate = function(d){ d=new Date(d.replace(/-/g, "/")); var f={}; f.toString = function( format ){ return ff(format,d) } return f;}; //new Object();function ff(format,d){ if(format && typeof format == "string") format = format.toLowerCase(); switch(format) { case "yyyy-mm-dd hh:mm:ss" : return "2009-03-26 12:00:00" case "yyyy-mm-dd hh:mm" : return "2009-03-26 12:00" case "yyyy-mm-dd" : return "2009-03-26" case "yyyy" : return "2009" case "mm" : return d.getMonth() + 1; default : return "2009-03-26 12:00:00" }}alert(myDate("2009-03-12").toString("mm")) 用String对象的substr()和substring()…提取字符串,这样子不知道能不能…符合楼主的意思 谢谢各位。我对于js里的闭包,原型...的东西感觉好模糊的,似懂非懂,得抽空系统的好好学习一下。我本来是在asp.net里用DataSet的GetXml把数据库的记录转换成xml,然后在js代码里用ajax调用时,把日期类型的按自己的要求去显示。可结果发现日期返回这样的格式2009-01-20T16:52:37.03+08:00(也可能日期为""),后来没办法,只有在js里加上处理,用正则把"T"替换掉,把秒后面的.及其后面的数据去掉,也不知道有没有其他好办法(服务器端asp.net的代码不考虑修改)观望一下,计划明天结帖... 51js上有人给出跟22楼类似的代码:<script type="text/javascript">//<![CDATA[function myDate(text){ return new MyDate(text);}var MyDate = function( text ){ this.rat = new String(text); this.rat = new Date(this.rat.replace(/-/g, "/")); this.isDateFlag = ! isNaN(this.rat); this.toString = function( format ){ if(! this.isDateFlag) return ""; if(format && typeof format == "string") format = format.toLowerCase(); switch(format) { case "yyyy-mm-dd hh:mm:ss" : return this.rat.getFullYear() + "-" + (this.rat.getMonth()<9 ? "0" : "") + (this.rat.getMonth() + 1) + "-" + (this.rat.getDate()<10 ? "0" : "") + this.rat.getDate() + " " + (this.rat.getHours()<10 ? "0" : "") + this.rat.getHours() + ":" + (this.rat.getMinutes()<10 ? "0" : "") + this.rat.getMinutes() + ":" + (this.rat.getSeconds()<10 ? "0" : "") + this.rat.getSeconds(); case "yyyy-mm-dd hh:mm" : return this.rat.getFullYear() + "-" + (this.rat.getMonth()<9 ? "0" : "") + (this.rat.getMonth() + 1) + "-" + (this.rat.getDate()<10 ? "0" : "") + this.rat.getDate() + " " + (this.rat.getHours()<10 ? "0" : "") + this.rat.getHours() + ":" + (this.rat.getMinutes()<10 ? "0" : "") + this.rat.getMinutes(); case "yyyy-mm-dd" : return this.rat.getFullYear() + "-" + (this.rat.getMonth()<9 ? "0" : "") + (this.rat.getMonth() + 1) + "-" + (this.rat.getDate()<10 ? "0" : "") + this.rat.getDate(); case "yyyy" : return this.rat.getFullYear(); case "mm" : return (this.rat.getMonth()<9 ? "0" : "") + (this.rat.getMonth() + 1); default : return this.rat.getFullYear() + "-" + (this.rat.getMonth()<9 ? "0" : "") + (this.rat.getMonth() + 1) + "-" + (this.rat.getDate()<10 ? "0" : "") + this.rat.getDate() + " " + (this.rat.getHours()<10 ? "0" : "") + this.rat.getHours() + ":" + (this.rat.getMinutes()<10 ? "0" : "") + this.rat.getMinutes() + ":" + (this.rat.getSeconds()<10 ? "0" : "") + this.rat.getSeconds(); } } //return this.rat;}alert( myDate("2009-03-26 12:30:00").toString() );alert( myDate("2009-03-26 12:30:00").toString("yyyy-mm-dd") );//]]></script> 我不是来和你讨论单例不单例,对象不对象的,我想说两个额外的问题:1:你的mm既表示月份,也表示分钟,是不好的,分钟就用nn更好,以免混淆。2:代码老长老长的,我不喜欢,简洁而不简单,是JS人的追求。俺给你整个精简的,功能还更强:function myDate(text){ return new MyDate2(text); //换了个不同的名字MyDate2,省得有故意引起混淆的嫌疑。}var MyDate2 = function( text ){ var d = new Date(text.replace(/-/g, "/")); this.toString = function( format ){ return isNaN(d) ? "" : (format ? format.toLowerCase() : "yyyy-mm-dd hh:nn:ss").replace(/yyyy/g, d.getFullYear()).replace(/mm/g, d.getMonth()+1).replace(/dd/g, d.getDate()).replace(/hh/g, d.getHours()).replace(/nn/g, d.getMinutes()).replace(/ss/g, d.getSeconds()).replace(/(?=[^\d])(\d)(?=[^\d])/g,'0$1'); }}//format的格式随便你写啦,有yyyy,mm,dd,hh,nn,ss的地方就替换掉。document.writeln( myDate("2009-03-26 12:30:00").toString() );document.writeln("<br>");document.writeln( myDate("2009-03-23 12:30:00").toString("yyyy-mm-dd") );document.writeln("<br>");document.writeln( myDate("2009-03-23 12:30:00").toString("yyyy年mm月dd日") ); csdn的代码不能自动换行?中间有空格也不换啊? 看了一下,toString是写的比较具体,但是我觉得代码封装性还不如我的简洁明了,#18 无聊凑个热闹,我人比较笨,N久以前用的字符串操作,呵呵....再加new Date()的构造函数来判断日期格式....var temp = "20009-1-2 12:13:14";//或者不带日期"2009-2-3"//replace(/-/g,"/");之前想过用这个,但是加了时间貌似就不能new Date()了......this.d = new Date(parseInt(temp.split("-")[0]),parseInt(temp.split("-")[1])-1,parseInt(temp.substring(temp.lastIndexOf("-")+1,temp.indexOf(" "))),parseInt(temp.substring(temp.indexOf(" ")+1,temp.indexOf(":"))),parseInt(temp.split(":")[1]),parseInt(temp.split(":")[2]));if(isNaN(this.d.getFullYear())){ this.d = new Date(parseInt(temp.split("-")[0]),parseInt(temp.split("-")[1])-1,parseInt(temp.split("-")[2])); if(isNaN(this.d.getFullYear())){ this.d = new Date();//2种都不匹配,我才赋值当前时间 } }}js的new Date()可以带的参数是:"month dd,yyyy hh:mm:ss" 如:"May 12,2009 11:12:13""month dd,yyyy" 如:"January 12,2009"yyyy,MM,dd,hh,mm,ss 太常见了,不用说了yyyy,MM,ddms 1970年1月1日到某个时间的毫秒数 谢谢cuixiping 的指点谢谢各位...结帐... 高手请进!十万火急! iframe跨域提交表单 后获取iframe的内容(访问拒绝) 已经几天了 关于jquery.scrollShow.js 正则表达式 参数 JS问题!急啊!!! firefox下js处理xpath问题,非常奇怪,高手出招啊 下拉框脚本。散:90分。 开新窗口问题 帮小弟看看这段程序!!! confirm FF下怎么无效,如何解决 js匹配任意字符功能 请教一下,JAVASCRIPT验证问题 关于延迟执行for循环的问题。
js里没mssql那么方便to chinmo :
我上面有作大小写处理,我这些代码没有错,只是想问一下,如何实现这样形式的写法:alert( myDate("2009-03-26 12:30:00").toString("yyyy-mm-dd") )
看别人写的格式化
myDate._d=new Date(d.replace(/-/g, "/"));
return myDate
}; //new Object();
myDate.toString = function( format ){
if(format && typeof format == "string")
format = format.toLowerCase(); switch(format) {
case "yyyy-mm-dd hh:mm:ss" :
return "2009-03-26 12:00:00"
case "yyyy-mm-dd hh:mm" :
return "2009-03-26 12:00"
case "yyyy-mm-dd" :
return "2009-03-26"
case "yyyy" :
return "2009"
case "mm" :
return myDate._d.getMonth() + 1;
default :
return "2009-03-26 12:00:00"
}
}alert(myDate("2009-03-12").toString("mm"))
不知道楼主的代码不要new object()而用new date()行不行?
d=new Date(d.replace(/-/g, "/"));
var f={};
f.toString = function( format ){
if(format && typeof format == "string")
format = format.toLowerCase();
switch(format) {
case "yyyy-mm-dd hh:mm:ss" :
return "2009-03-26 12:00:00"
case "yyyy-mm-dd hh:mm" :
return "2009-03-26 12:00"
case "yyyy-mm-dd" :
return "2009-03-26"
case "yyyy" :
return "2009"
case "mm" :
return d.getMonth() + 1;
default :
return "2009-03-26 12:00:00"
}
}
return f;
}; //new Object();
alert(myDate("2009-03-12").toString("mm"))这就能解决
扩展系统的Date,我开始有想到过,后来感觉还是另外再写。to chinmo :
您给的别人格式化那种方式,我见过,是一种不错的思路。to cloudgamer :
第一种方法感觉不错
return myDate后不需要用prototype定义function
单例有什么不好吗??????? (显然单例模式的要点有三个;一是某各类只能有一个实例;二是它必须自行创建这个事例;三是它必须自行向整个系统提供这个实例。) 您的第二种方法也可以实现我说的效果,但如果以后要继续扩展属性,方法,感觉就没办法了????
var myDate = function(d){
myDate._d=new Date(d.replace(/-/g, "/"));
return myDate
}; //new Object();
myDate.toString = function( format ){
if(format && typeof format == "string")
format = format.toLowerCase(); switch(format) {
case "yyyy-mm-dd hh:mm:ss" :
return "2009-03-26 12:00:00"
case "yyyy-mm-dd hh:mm" :
return "2009-03-26 12:00"
case "yyyy-mm-dd" :
return "2009-03-26"
case "yyyy" :
return "2009"
case "mm" :
return myDate._d.getMonth() + 1;
default :
return "2009-03-26 12:00:00"
}
}//新加的
myDate.fun=function(){
return myDate._d;
}alert(myDate("2009-03-12").toString("mm"))
alert(myDate("2009-03-12").fun())var myDate = function(d){
d=new Date(d.replace(/-/g, "/"));
var f={};
f.toString = function( format ){
if(format && typeof format == "string")
format = format.toLowerCase();
switch(format) {
case "yyyy-mm-dd hh:mm:ss" :
return "2009-03-26 12:00:00"
case "yyyy-mm-dd hh:mm" :
return "2009-03-26 12:00"
case "yyyy-mm-dd" :
return "2009-03-26"
case "yyyy" :
return "2009"
case "mm" :
return d.getMonth() + 1;
default :
return "2009-03-26 12:00:00"
}
}
//新加的
f.fun = function(){
return d
}
return f;
}; //new Object();alert(myDate("2009-03-12").toString("mm"))
alert(myDate("2009-03-12").fun()) 但这样的问题是只能单例
----------------------------
按你的意思是这儿用单例不好,为什么不好呢,麻烦你讲解一下??
var t1 = myDate("2009-03-12");
var t2 = myDate("2009-04-12");
alert(t1.toString());呵呵,cloudgamer的代码很好的解决了楼主的问题,不过每都是一个新的toStringfunction太浪费了
我也来贴一种实现方式,<script type="text/javascript">(function(){
var myDate = window.myDate = function(d){
return new fn.init(d);
}; //new Object();
var fn = myDate.prototype = {
init: function(d){
this.date = new Date(d.replace(/-/g, "/"));
},
toString: function( format ){
if(format && typeof format == "string")
format = format.toLowerCase();
switch(format) {
case "yyyy-mm-dd hh:mm:ss" :
return "2009-03-26 12:00:00"
case "yyyy-mm-dd hh:mm" :
return "2009-03-26 12:00"
case "yyyy-mm-dd" :
return "2009-03-26"
case "yyyy" :
return "2009"
case "mm" :
return this.date.getMonth() + 1;
default :
return "2009-03-26 12:00:00"
}
},
valueOf:function(){
return this.date.valueOf();
}
}
fn.init.prototype = fn;
})();var myDate1 = new myDate("2009-03-12"); //新建对象的方式1
var myDate2 = myDate("2009-04-12"); //新建对象的方式2
alert(myDate1.toString("mm"));
alert(myDate2.valueOf());
</script>
你代码里的myDate.prototype = 是不是可以不要,我去掉显示结果一样,不知道你的用意fn.init.prototype = fn;
这句不明白意思?
fn.init是一个function, function.prototype = obj 仅此而已
var myDate = function(d){
return new f(d);
}; //new Object();function f(d){
this._d=new Date(d.replace(/-/g, "/"));
}
f.prototype.toString = function( format ){
if(format && typeof format == "string")
format = format.toLowerCase(); switch(format) {
case "yyyy-mm-dd hh:mm:ss" :
return "2009-03-26 12:00:00"
case "yyyy-mm-dd hh:mm" :
return "2009-03-26 12:00"
case "yyyy-mm-dd" :
return "2009-03-26"
case "yyyy" :
return "2009"
case "mm" :
return this._d.getMonth() + 1;
default :
return "2009-03-26 12:00:00"
}
}alert(myDate("2009-03-12").toString("mm"))
只是把new放到函数里面
var myDate = function(d){
d=new Date(d.replace(/-/g, "/"));
var f={};
f.toString = function( format ){ return ff(format,d) }
return f;
}; //new Object();function ff(format,d){
if(format && typeof format == "string")
format = format.toLowerCase();
switch(format) {
case "yyyy-mm-dd hh:mm:ss" :
return "2009-03-26 12:00:00"
case "yyyy-mm-dd hh:mm" :
return "2009-03-26 12:00"
case "yyyy-mm-dd" :
return "2009-03-26"
case "yyyy" :
return "2009"
case "mm" :
return d.getMonth() + 1;
default :
return "2009-03-26 12:00:00"
}
}alert(myDate("2009-03-12").toString("mm"))
我对于js里的闭包,原型...的东西感觉好模糊的,似懂非懂,得抽空系统的好好学习一下。我本来是在asp.net里用DataSet的GetXml把数据库的记录转换成xml,然后在js代码里用ajax调用时,把日期类型的按自己的要求去显示。可结果发现日期返回这样的格式2009-01-20T16:52:37.03+08:00(也可能日期为""),后来没办法,只有在js里加上处理,用正则把"T"替换掉,把秒后面的.及其后面的数据去掉,也不知道有没有其他好办法(服务器端asp.net的代码不考虑修改)
观望一下,计划明天结帖...
//<![CDATA[
function myDate(text){
return new MyDate(text);
}var MyDate = function( text ){
this.rat = new String(text);
this.rat = new Date(this.rat.replace(/-/g, "/"));
this.isDateFlag = ! isNaN(this.rat); this.toString = function( format ){
if(! this.isDateFlag)
return ""; if(format && typeof format == "string")
format = format.toLowerCase(); switch(format) {
case "yyyy-mm-dd hh:mm:ss" :
return this.rat.getFullYear()
+ "-" + (this.rat.getMonth()<9 ? "0" : "") + (this.rat.getMonth() + 1)
+ "-" + (this.rat.getDate()<10 ? "0" : "") + this.rat.getDate()
+ " " + (this.rat.getHours()<10 ? "0" : "") + this.rat.getHours()
+ ":" + (this.rat.getMinutes()<10 ? "0" : "") + this.rat.getMinutes()
+ ":" + (this.rat.getSeconds()<10 ? "0" : "") + this.rat.getSeconds();
case "yyyy-mm-dd hh:mm" :
return this.rat.getFullYear()
+ "-" + (this.rat.getMonth()<9 ? "0" : "") + (this.rat.getMonth() + 1)
+ "-" + (this.rat.getDate()<10 ? "0" : "") + this.rat.getDate()
+ " " + (this.rat.getHours()<10 ? "0" : "") + this.rat.getHours()
+ ":" + (this.rat.getMinutes()<10 ? "0" : "") + this.rat.getMinutes();
case "yyyy-mm-dd" :
return this.rat.getFullYear()
+ "-" + (this.rat.getMonth()<9 ? "0" : "") + (this.rat.getMonth() + 1)
+ "-" + (this.rat.getDate()<10 ? "0" : "") + this.rat.getDate();
case "yyyy" :
return this.rat.getFullYear();
case "mm" :
return (this.rat.getMonth()<9 ? "0" : "") + (this.rat.getMonth() + 1);
default :
return this.rat.getFullYear()
+ "-" + (this.rat.getMonth()<9 ? "0" : "") + (this.rat.getMonth() + 1)
+ "-" + (this.rat.getDate()<10 ? "0" : "") + this.rat.getDate()
+ " " + (this.rat.getHours()<10 ? "0" : "") + this.rat.getHours()
+ ":" + (this.rat.getMinutes()<10 ? "0" : "") + this.rat.getMinutes()
+ ":" + (this.rat.getSeconds()<10 ? "0" : "") + this.rat.getSeconds();
}
}
//return this.rat;
}
alert( myDate("2009-03-26 12:30:00").toString() );
alert( myDate("2009-03-26 12:30:00").toString("yyyy-mm-dd") );//]]>
</script>
1:你的mm既表示月份,也表示分钟,是不好的,分钟就用nn更好,以免混淆。
2:代码老长老长的,我不喜欢,简洁而不简单,是JS人的追求。俺给你整个精简的,功能还更强:function myDate(text){
return new MyDate2(text); //换了个不同的名字MyDate2,省得有故意引起混淆的嫌疑。
}
var MyDate2 = function( text ){
var d = new Date(text.replace(/-/g, "/"));
this.toString = function( format ){
return isNaN(d) ? "" : (format ? format.toLowerCase() : "yyyy-mm-dd hh:nn:ss").replace(/yyyy/g, d.getFullYear()).replace(/mm/g, d.getMonth()+1).replace(/dd/g, d.getDate()).replace(/hh/g, d.getHours()).replace(/nn/g, d.getMinutes()).replace(/ss/g, d.getSeconds()).replace(/(?=[^\d])(\d)(?=[^\d])/g,'0$1');
}
}
//format的格式随便你写啦,有yyyy,mm,dd,hh,nn,ss的地方就替换掉。
document.writeln( myDate("2009-03-26 12:30:00").toString() );
document.writeln("<br>");
document.writeln( myDate("2009-03-23 12:30:00").toString("yyyy-mm-dd") );
document.writeln("<br>");
document.writeln( myDate("2009-03-23 12:30:00").toString("yyyy年mm月dd日") );
var temp = "20009-1-2 12:13:14";//或者不带日期"2009-2-3"
//replace(/-/g,"/");之前想过用这个,但是加了时间貌似就不能new Date()了......
this.d = new Date(parseInt(temp.split("-")[0]),parseInt(temp.split("-")[1])-1,parseInt(temp.substring(temp.lastIndexOf("-")+1,temp.indexOf(" "))),parseInt(temp.substring(temp.indexOf(" ")+1,temp.indexOf(":"))),parseInt(temp.split(":")[1]),parseInt(temp.split(":")[2]));if(isNaN(this.d.getFullYear())){
this.d = new Date(parseInt(temp.split("-")[0]),parseInt(temp.split("-")[1])-1,parseInt(temp.split("-")[2]));
if(isNaN(this.d.getFullYear())){
this.d = new Date();//2种都不匹配,我才赋值当前时间
}
}
}js的new Date()可以带的参数是:
"month dd,yyyy hh:mm:ss" 如:"May 12,2009 11:12:13"
"month dd,yyyy" 如:"January 12,2009"
yyyy,MM,dd,hh,mm,ss 太常见了,不用说了
yyyy,MM,dd
ms 1970年1月1日到某个时间的毫秒数
谢谢各位...结帐...