关于js格式化时间戳,js中时间格式化转换这个很多人还不知道,今天小栢来为大家解答以上的问题,现在让我们一起来看看吧!

js格式化时间戳 js中时间格式化转换js格式化时间戳 js中时间格式化转换


js格式化时间戳 js中时间格式化转换


1、var date2 = new Date(“2017/06/06”); console.log(date2);对 new Date() 得到日期的进行格式显示扩展,扩展方法如下:var o = {"M+": this.getMonth() + 1, //月份"h+": this.getHours(), //小时"m+": this.getMinutes(), //分"s+": this.getSeconds(), //秒"q+": Math.floor((this.getMonth() + 3) / 3), //季度};RegExp.$1.length));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;}使用例子:(new Date()).Format("yyyy-MM-dd hh:mm:ss.S") //输出结果: 2017-01-23 09:36:10.400(new Date()).Format("yyyy-M-d h:m:s.S") //输出结果: 2017-1-23 9:36:35.572扩展资料JS Date对象常用的带参数初始化方式:1、用整数初始化日期对象var date1 = new Date(2017,06,06); console.log(date1); // Thu Jul 06 2017 00:00:00 GMT+0800 (标准时间)var date1 = new Date(2017,1,1); console.log(date1); // Wed Feb 01 2017 00:00:00 GMT+0800 (标准时间)var date1 =new Date(2017,06,06,06,06,06); console.log(date1); // Thu Jul 06 2017 06:06:06 GMT+0800 (标准时间)说明: new Date( year, month, date, hrs, min, sec) 按给定的参数创建一日期对象2、用字符串初始化日期对象var date2 = new Date(“2017/06/06”); console.log(date2); // Tue Jun 06 2017 00:00:00 GMT+0800 (标准时间)var date2 = new Date(“2017-08-08”); console.log(date2); // Tue Aug 08 2017 08:00:00 GMT+0800 (标准时间)var date2 = new Date(“2017-9-9”); console.log(date2); // Sat Sep 09 2017 00:00:00 GMT+0800 (标准时间)说明:如果字符串模式不支持短横杠模式,则进行字符串替换:var datconsole.log(Result)e2= new Date(Date.parse(strTime.replace(/-/g, “/”))); // /-/g为正则表达式(RegExp) 对象,表示全局替换-为/。

2、new Date()参数格式如下:1、用整数初始化日期对象var date1 = new Date(2017,06,06); console.log(date1);表示的是:Thu Jul 06 2017 00:00:00 GMT+0800 (标准时间) 。

3、new Date( year, month, date, hrs, min, sec) 按给定的参数创建一日期对象2、用字符串初始化日期对象表示的是:Tue Jun 06 2017 00:00:00 GMT+0800 (标准时间) ,如果字符串模式不支持短横杠模式,则进行字符串替换:var date2= new Date(Date.parse(strTime.replace(/-/g, “/”))); // /-/g为正则表达式(RegExp) 对象,表示全局替换-为/。

4、3、用毫秒时间戳初始化日期对象var timestamp=new Date().getTime(); console.log( new Date(timestamp) );表示的是:Tue Jun 06 2017 11:06:59 GMT+0800 (标准时间) ,时间戳是指格林威治时间1970年01月01日00时00分00秒(时间1970年01月01日08时00分00秒)起至现在的总秒数。

5、时间戳识某一刻的时间。

6、扩展资料:一般直接new Date() 是不会出现兼容性问题的,而 new Date(datetimeformatstring) 常常会出现浏览器兼容性问题,因为datetimeformatstring中的某些格式浏览器不兼容。

7、其中无参方式所有浏览器都兼容:var dateTime = new Date();目前所有主流浏览器都支持的有参格式为: var dateTime = new Date("2017/09/12 13:42:00");所以出现浏览器不兼容的问题可以将datetimeformatstring 转换成 yyyy/MM/dd hh:mm:ss 格式化字符串即可。

8、1.Date 对象用于处理日期和时间。

9、创建 Date 对象的语法:var myDate=new Date()Date 对象会自动把当前日期和时间保存为其初始值。

10、2.参数形式有以下5种:new Date("month dd,yyyy hh:mm:ss");new Date("month dd,yyyy");new Date(yyyy,mth,dd,hh,mm,ss);一种形式,参数表示需要创建的时间和GMT时间1970年1月1日之间相的毫秒数。

11、3.各种函数的含义如下:month:用英文表示月份名称,从January到Decemberdd:表示一个月中的第几天,从1到31yyyy:四位数表示的年份hh:小时数,从0(午夜)到23(晚11点)mm:分钟数,从0到59的整数ss:秒数,从0到59的整数ms:毫秒数,为大于等于0的整数如:new Date("January 12,2006 22:19:35");new Date("January 12,2006");new Date(2006,0,12,22,19,35);new Date(2006,0,12);new Date(1137075575000);Date() 返回当日的日期和时间。

12、getDate() 从 Date 对象返回一个月中的某一天 (1 ~ 31)。

13、getDay() 从 Date 对象返回一周中的某一天 (0 ~ 6)。

14、getMonth() 从 Date 对象返回月份 (0 ~ 11)。

15、getFullYear() 从 Date 对象以四位数字返回年份。

16、getYear() 请使用 getFullYear() 方法代替。

17、getHours() 返回 Date 对象的小时 (0 ~ 23)。

18、getMinutes() 返回 Date 对象的分钟 (0 ~ 59)。

19、getSeconds() 返回 Date 对象的秒数 (0 ~ 59)。

20、getMilliseconds() 返回 Date 对象的毫秒(0 ~ 999)。

21、getTime() 返回 1970 年 1 月 1 日至今的毫秒数。

22、getTimezoneOffset() 返回本地时间与格林威治标准时间 (GMT) 的分钟。

23、getUTCDate() 根据世界时从 Date 对象返回月中的一天 (1 ~ 31)。

24、getUTCDay() 根据世界时从 Date 对象返回周中的一天 (0 ~ 6)。

本文到这结束,希望上面文章对大家有所帮助。