Javascript - Formatting Date string to YYYY-MM-DD - Stack Overflow

I need to express a Date as a String with the exact format format:2015-01-20 I see there is an date.t

I need to express a Date as a String with the exact format format:

 2015-01-20   

I see there is an date.toIsoFormatString() but not sure where or how to provide the format shown above.

 Here is my code:

 var future = new Date();
 future.setDate(future.getDate() + 30);
 console.log(future);

Now I want future.toString() or future.toIsoFormatString() to display the date in just the YYYY-MM-DD format.

I need to express a Date as a String with the exact format format:

 2015-01-20   

I see there is an date.toIsoFormatString() but not sure where or how to provide the format shown above.

 Here is my code:

 var future = new Date();
 future.setDate(future.getDate() + 30);
 console.log(future);

Now I want future.toString() or future.toIsoFormatString() to display the date in just the YYYY-MM-DD format.

Share Improve this question asked Jan 30, 2015 at 22:50 Get SmarterGet Smarter 1912 gold badges3 silver badges9 bronze badges 6
  • 2 And any of these answers or the documentation couldn't solve your problem? – Teemu Commented Jan 30, 2015 at 22:51
  • No Teemu. No console.log(future.toString('yyyy-MM-dd')); taken directly from this answer yields: Sun Mar 01 2015 15:01:59 GMT-0800 (PST), but if you can think of an answer please post it. – Get Smarter Commented Jan 30, 2015 at 23:03
  • No? Just keep digging ... ; ) – Teemu Commented Jan 30, 2015 at 23:06
  • www.stackoverflow. --> www.justkeepdigging. ? :) – Get Smarter Commented Jan 30, 2015 at 23:12
  • Guess I've had too much Vodka ... – Get Smarter Commented Jan 30, 2015 at 23:14
 |  Show 1 more ment

2 Answers 2

Reset to default 6

IE>8 and all other updated browsers can do this natively-

new Date().toISOString().slice(0,10);

You can use momentJs library for formated date.

Example without momentJs :

 myDate = new Date();
 myDate.getFullYear() + "-" + (myDate.getMonth()+1) + "-" + myDate.getDate() // 2015-1-31

Example with momentJs :

moment().format("YYYY-MM-DD") // 2015-01-31

MomentJs have many others useful function

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743736258a4498318.html

相关推荐

  • Javascript - Formatting Date string to YYYY-MM-DD - Stack Overflow

    I need to express a Date as a String with the exact format format:2015-01-20 I see there is an date.t

    6天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信