在JavaScript中,会不会有像在msSQL那样格式花时间表达的方法存在?
解决方案 »
- 如何得到 window.open 新页面中的数据
- 网站收藏功能在firefox下为什么不起作用
- 如何让层在下拉框上面?
- 无法打开Internet站点 已终止操作 ----这种问题是怎么回事?
- 当点击"修改密码"文字时,用js如何实现 弹出一个小的对话框提示"请输入原密码"输入完原密码点击确定后再弹出对话框提示"请输入修改后密码"
- 100分求免费版电子书,分不够再加。
- 怎么用javascri语言t登陆窗口输入账号密码后怎么调到主界面去
- 请高手指点
- 用createElement创建的对象如何定义事件
- 有无办法可以使顶部frame的菜单条不被盖住??
- 请教一下,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日到某个时间的毫秒数
谢谢各位...结帐...