Javascript日期格式化 [英] Javascript date formatting

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

问题描述

我已经尝试过我的最佳水平,但无法得到结果。我需要的是YYYY-MM-DD格式的日期。这是我现在的代码:

I've tried my level best but couldn't get the result. What I need is the date in YYYY-MM-DD format. Here is my current code:


function AddDays(days) {

            var thisDate = new Date();
            thisDate.setDate(thisDate.getDate() + days);
            var dd = ( thisDate.getFullYear() + '-' + (thisDate.getMonth() + 1) + '-' + thisDate.getDate() );
            return dd;

        }

        alert(AddDays(365));


我得到2014-8-7但我想把它当YYYY-MM-DD。我需要它看起来像2014-08-07。

I'm getting "2014-8-7" as result but I want to get it as YYYY-MM-DD. I need it to look like "2014-08-07".

推荐答案

var today = new Date();
console.log(today.getFullYear()+'-'+((today.getMonth()+1>9)?today.getMonth():'0'+(today.getMonth()+1))+'-'+(today.getDate()<10?'0'+today.getDate():today.getDate()));

小提琴为:
http://jsfiddle.net/Ge4cw/

以前还有以下问题: -

It has also been previously asked:-

获取字符串来自JS日期对象的YYYYMMDD格式?

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

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