1. 首页 > 汽车 >

js new date 格式化 js中格式化日期

js日期转换格式的方法ie不支持,提示对象不支持此属性或方法,求解决方法。。。火狐、360都没问题的。。

var day = parseInt(newtimestamp / (24 60 60 1000)); //除以一天的毫秒数,得出间隔天数

function getNowFormatDate()

js new date 格式化 js中格式化日期js new date 格式化 js中格式化日期


{var day = new Date();

var Year = 0;

var Month = 0;

var Day = 0;

var CurrentDate = "";

//初始化时间

//Year= day.getYear();//有火狐下2008年显示108的bug

Month= day.getMonth()+1;

Day = day.getDate();

//Hour = day.getHours();

// Minute = day.getMinutes();

CurrentDate += Year + "-";

if (Month >= 10 )

{CurrentDate += Month + "-";

}else

}if (Day >= 10 )

{CurrentDate += Day ;

}else

{CurrentDate += "0" + Day ;

}return CurrentDate;

}这个来试if( (n = jsdate.getMonth() + 1) == 2 ){试

ISO Date Format is not supported in Internet Explorer 8 standards mode and Quirks mode.

格林威治时间转换为系统时间(js)

}调用方法 能够很方便的将时间戳转换成日期的格式,如:

//日期格式化

Date.prototype.Format = function (fmt) { //author: meizz

var o = {

"Mdate('Y-m-d H:i:s','1350052653');//得到的结果是2012-10-12 22:37:33+": 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() //毫秒

};

fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));

for (var k in o)

fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));

return fmt;

}//

//输出测试

var _time = new Date();

console.log(_time.Format("yyyy-MM-dd hh:mm:ss"))

js时间戳怎么转成日期格式

if ((String(beat)).length == 2) beat = "0"+beat;

一.js将时间转换成时间戳

1.js获取当前时间戳的方法

var timestamp1 = Date.parse(new Date());

var timestamp2 = (new Date()).valueOf();

var timestamp3 = new Date().getTime();

2.js获取制定时间戳的方法

var oldTime = (new Date("2015/06/23 08:00:20")).getTime()/1000;

getTime()返回数值的单位是毫秒。

二.js把时间戳转为为普通日期格式

1.Date toLocaleString方法

function getLocalTime(nS) {

return new Date(parseInt(nS) 1000).toLocaleString().replace(/:\d{1,2}$/,' ');

toLocaleString() 方法可根据本地时间把 Date 对象转换为字符串,并返回结果。

replace() 方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串。

replace(/:\d{1,2}$/,' ')验证替换以:开始有一位或二位数字的结束字符串,就是秒;替换为空

显示如下:

所以我们可以利用正则表达式改变我们想要的日期格式。

2.Date}strDay = Integer.toString(days); 属性方法

function add0(m){return m<10?'0'+m:m }

function format(shijianchuo)

var time = new Date(shijianchuo);

var y = time.getFullYear();

var d = time.getDate();

var h = time.getHours();

var mm = time.getMinutes();

var s = time.getSeconds();

return y+'-'+add0(m)+'-'+add0(d)+' '+add0(h)+':'+add0(mm)+':'+add0(s);

}三.封装的时间格式器

/

和PHP一样的时间戳格式化函数

@param {string} format 格式

@param {int} timestamp 要格式化的时间 默认为当前时间

@return {string} 格式化的时间字符串

/

function date(format, timestamp){

var a, jsdate=((timestamp) ? new Date(timestamp1000) : new Date());

var pad = function(n, c){

if((n = n + "").length < c){

return new Array(++c - n.length).join("0") + n;

} else {

return n;

}};

var txt_weekdays = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];

var txt_ordin = {1:"st", 2:"nd", 3:"rd", 21:"st", 22:"nd", 23:"rd", 31:"st"};

var txt_months = ["", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];

var f = {

// Day

d: function(){return pad(f.j(), 2)},

D: function(){return f.l().substr(0,3)},

j: function(){return jsdate.getDate()},

l: function(){return txt_weekdays[f.w()]},

N: function(){return f.w() + 1},

S: function(){return txt_ordin[f.j()] ? txt_ordin[f.j()] : 'th'},

w: function(){return jsdate.getDay()},

z: function(){return (jsdate - new Date(jsdate.getFullYear() + "/1/1")) / 864e5 >> 0},

// Week

W: function(){

var nd2, nd = (new Date(jsdate.getFullYear() + "/1/1").getDay() || 7) - 1;

if(b <= 2 && ((jsdate.getDay() || 7) - 1) <= 2 - b){

return 1;

} else{

if(a <= 2 && nd >= 4 && a >= (6 - nd)){

nd2 = new Date(jsdate.getFullYear() - 1 + "/12/31");

return date("W", Math.round(nd2.getTime()/1000));

} else{

return (1 + (nd <= 3 ? ((a + nd) / 7) : (a - (7 - nd)) / 7) >> 0);

}}

// Month

F: function(){return txt_months[f.n()]},

m: function(){return pad(f.n(), 2)},

M: function(){return f.F().substr(0,3)},

n: function(){return jsdate.getMonth() + 1},

var n;

return 28 + f.L();

} else{

if( n & 1 && n < 8 || !(n & 1) && n > 7 ){

return 31;

} else{

}}

// Year

L: function(){var y = f.Y();return (!(y & 3) && (y % 1e2 || !(y % 4e2))) ? 1 : 0},

//o not supported yet

Y: function(){return jsdate.getFullYear()},

y: function(){return (jsdate.getFullYear() + "").sl(2)},

// Time

a: function(){return jsdate.getHours() > 11 ? "pm" : "am"},

A: function(){return f.a().toUpperCase()},

var off = (jsdate.getTimezoneOffset() + 60)60;

var theSeconds = (jsdate.getHours() 3600) + (jsdate.getMinutes() 60) + jsdate.getSeconds() + off;

var beat = Math.floor(theSeconds/86.4);

if (beat > 1000) beat -= 1000;

if (beat < 0) beat += 1000;

return beat;

G: function(){return jsdate.getHours()},

H: function(){return pad(jsdate.getHours(), 2)},

i: function(){return pad(jsdate.getMinutes(), 2)},

s: function(){return pad(jsdate.getSeconds(), 2)},

//u not supported yet

// Timezone

//e not supported yet

O: function(){

var t = pad(Math.abs(jsdate.getTimezoneOffset()/60100), 4);

if (jsdate.getTimezoneOffset() > 0) t = "-" + t; else t = "+" + t;

return t;

P: function(){var O = f.O();return (O.substr(0, 3) + ":" + O.substr(3, 2))},

//T not supported yet

//Z not supported yet

// Full Date/Time

c: function(){return f.Y() + "-" + f.m() + "-" + f.d() + "T" + f.h() + ":" + f.i() + ":" + f.s() + f.P()},

//r not supported yet

};

return format.replace(/[\]?([a-zA-Z])/g, function(t, s){

if( t!=s ){

// escaped

ret = s;

} else if( f[s] ){

// a date function exists

ret = f[s]();

} else{

// nothing special

ret = s;

}return ret;

date('Y-m-d','1350052653');//很方便的将时间戳转换成了2012-10-11

js中怎么日月年的日期格式转化为年月日的格式,如果是new Date().format那个方法就算

{CurrentDate += "0" + Month + "-";

//27-2月-2020转换为2020-2-27

function getDate(str)

{var strArr = str.split('-');

var day=strArr[0];

var month=strArr[1].replace("月",'');

var year=strArr[2];

date = (year + '-' + month + '-' + day).toString();

return datA string representing a date.e;

}要换成日期格式再转换下就可以了

jquery 格式化时间

{var fTime, fStr = 'ymdhis';

js 字符串类型转换成时间类型:

一般是根据空格( ),冒号(:)分割字符串,然后

var dd=new Date("1998","11","29","10","11","44");

如果是上面的格式,直接转化即可: var dd=new Date("Tue, js中时间作单位是毫秒。09 Nov 2010 14:56:53 GMT");

你把后面的小时分秒去掉试试

input组件怎么设置当天日期作为默认值显示

{//shijianchuo是整数,否则要parseInt转换

问题是,你使用了它们后,发现日期并不是默认在当天的(没有默认任何日期),而且el.value=new Date()也不行,只支持-MM-ddTHH:mm:ss.SSS(或其部分)这种形式的的字符串:

在ja中处理日期比js要方便,处理成如“2011-9-27”后再转成字符串,传值给js

From the documentation:

js默认是没有日期格式化函数的,除非你打算:

引入一个js的日期库

自己写一个简单的日期扩展类(自己考虑每月天数的不同,闰年等等问题,以实现日期的增减)

用后台语言直接在value的位置格式化输出,如

在这篇文章找到了一个简单的用法,文档在此,使用:

js中 var time = new Date().getTime()得到的是毫秒数,怎么转成日期型年月日格式

U: function(){return Math.round(jsdate.getTime()/1000)}

Date.prototype.format = function(fmt){

var year = this.getFullYear();

var month = this.getMonth()+1;

var date = this.getDate();

var hour = this.getHours();

varif ((String(beat)).length == 1) beat = "00"+beat; minute = this.getMinutes();

var second = this.getSeconds();

fmt = fmt.replace("yyyy",year);

fmt = fmt.replace("yy",year%100);

fmt = fmt.replace("MM",fix(month));

fmt = fmt.replace("dd",fix(this.getDate()));

fmt = fmt.replace("hh",fix(this.getHours()));

fmt = fmt.replace("mm",fix(this.getMinutes()));

fmt = fmt.replace("ss",fix(this.getSeconds()));

return fmt;

function fix(n){

return n<10?"0"+n:n;

}}

var time = new Date().format("yyyy年MM月dd日 hh:mm:ss")

alert(time)

JSP里将日期时间类型转化为日期类型输出

return 30;

js 代码

strDay1 = "0" + strDay1;

Date.prototype.format = function(format){

var o = {

"M+" : this.getMonth()+1, //month

"d+" : this.getDate(), //day

"h+" : this.getHours(), //hour

"m+" : this.getMinutes(), //minute

"s+" : this.getSeconds(), //second

"q+" : Math.floor((this.getMonth()+3)/3), //quarter

"S" : this.getMilliseconds() //millisecond

};

if(/(y+)/.test(format)) {

format = format.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));

if(new RegExp("("+ k +")").test(format)) {

format = format.replace(RegExp.$1, RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));

}};

return format;

};

使用方法

var nowStr = now.format("yyyy年MM月dd日hh小时mm分ss秒");

now.format("yyyy-MM-dd hh:mm:ss");

JS 的时间格式

if (/(y+)/.test(fmt))

一直被各种时间格式困扰,所以这次来总结一下常用的时间格式到底有哪些,它们之间如何互相转换。

ISO Date "2015-03-25" 标准

Long Date "Mar 25 2015" 或者 "25 Mar 2015"

Full Date "Wednesday March 25 2015"

new Date();方法可以将前四种时间格式当做参数,输出 UTC 格式的时间

UTC "2015-03-25T12:00:00" 看到日期和时间中间有一个字母 T 表示为 UTC 时间 是 现在使用的标准时间

UTC 是时间标准;ISO-8601 是表示时间的一种标准格式

ISO-8601 的具体日期的时间表示法,在 UTC 时间后加 Z,如 "2015-03-25T12:00:00Z",Z 表示是通用标准,其它的时区的时间和 UTC 不同,所以用实际时间加时表示。

GMT Greenwich Mean Time "Mon Feb 13 08:00:00 GMT+08:00 2012" 中间有 GMT+ 字样

UTC(Universal Time Coordinated 原子钟提供)和 GMT (Greenwich Mean Time 格林尼治时间)一样

EDT (US)Easte// peter paul koch:rn Daylight Time 东部夏令时

CDT (US) Central Daylight Time 中部夏令时

MDT (US)Mountain Daylight Time 山地夏令时

PDT (US) Pacific Daylight Time 太平洋夏令时

EST (US) Eastern Standard Time 东部标准时间

CST "Web Mar 25 2015 08:00:00 GMT+0800" 中部标准时间

MST (US) Mountain Standard Time 山准时间

PST (US) Pacific Standard Time 太平洋标准时间

设置时间时,没有指定 time zone,js 将会使用浏览器的 time zone

获取时间时,没有指定 time zone,结果将会转换成浏览器的 time zone

总之,如果时间是用 GMT 时间创建,如果浏览器 from central US 时间将会被转换成 CDT。

涉及到格式类型:UTC,GMT,unix timestamp

js 里如何系统时间加一

g: function(){return jsdate.getHours() % 12 || 12},

可以使用:

function formatDate(formatStr, fdate)

var curDate = new Date();

var newDate=new Date(curDate.setDate(curDate.getDate()+1))来实现;

参考例子如下:

<%

//取得日期

String strMon,strDay,strDay1,dt,dt3;

ja.util.Date date1=new ja.util.Date();

Calendar calendar=Calendar.getInstance();

calendar.setTime(date1);

int year = calendar.get(Calendar.YEAR);

int mont = calendar.get(Calendar.MONTH)+1;

int days = calendar.get(Calendar.DATE);

int days1 = calendar.get(Calendar.DATE)+1;

strMon =Integer.toString(mont);

if(strMon.length() == 1) {

strMon = "0" + strMon;

if(strDay.length() == 1) {

strDay = "0" + strDay;

}strDay1 = Integer.toString(days1);

if(strDay1.length() == 1) {

}dt = Integer.toString(year) +"-" + strMon +"-" + strDay;

dt3 = Integer.toString(year) +"-" + strMon +"-" + strDay1;

out.println(dt);

out.println("-----");

out.println(dt3);

%>dt为今天的日期。

dt3为加1后的日期。

页面时间格式化

B: function(){

if (!formatStr)

}for(var k in o) {

formatStr= "y-m-d h:i:s";

if (fdate)

fTime = new Date(fdate);

else

fTime = new Date();

var formatArr = [

fTime.getFullYear().toString(),

(fTime.getMonth()+1).toString(),

fTime.getDate().toString(),

fTime.getHours().toString(),

fTime.getMinutes().toString(),

fTime.getSeconds().toString()

]for (var i=0; i

{formatStr = formatStr.replace(fStr.charAt(i), formatArr[i]);

}return formatStr;

}var now = new Date();

var nowStr = formatDate("y-m-d h:i:s",now);

$("#nowTime").text(nowStr);

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 12345678@qq.com 举报,一经查实,本站将立刻删除。

联系我们

工作日:9:30-18:30,节假日休息