Javascript日期格式,如ISO但本地 [英] Javascript date format like ISO but local

查看:67
本文介绍了Javascript日期格式,如ISO但本地的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何格式化像ISO格式的javascript日期,但是在当地时间?

how do I format a javascript date like ISO format, but in local time?

myDate.toISOString()我得到的时间是:2012-09-13T19:12:23.826Z

with myDate.toISOString() I am getting the time as: "2012-09-13T19:12:23.826Z"

但是在这里,它是22:13,所以我该怎么办以上述格式包含时区?

but here, it is 22:13, so how do I include the timezone in above format?

我最终做了......

I ended up doing...

pad=function(e,t,n){n=n||"0",t=t||2;while((""+e).length<t)e=n+e;return e}
c = new Date()
c.getFullYear()+"-"+pad(c.getMonth()+1)+"-"+pad(c.getDate()-5)+"T"+c.toLocaleTimeString().replace(/\D/g,':')+"."+pad(c.getMilliseconds(),3)


推荐答案

AFAIK你不能用javascript格式化日期(没有使用外部库)。你能做的最好的事情就是自己格式化。我的意思是:

AFAIK you can't format dates in javascript (without using external libraries). The best you could do is "format it yourself". I mean:

var date = new Date();
var year = date.getFullYear();
var month = date......


var ISOdate = year + "-" + month + "-" + .... ;

但是有一些好的库可以让你格式化日期! (读取格式,如 library.getDate(YYYY-MM-DD .........);

But there are some good libraries that will let you format dates! (read "format" as in library.getDate("YYYY-MM-DD.........");)

编辑:

Moment.js似乎是您正在寻找的东西: http://momentjs.com/

Moment.js seems the thing you're looking for: http://momentjs.com/

这篇关于Javascript日期格式,如ISO但本地的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆