admin管理员组文章数量:1431413
Im trying to get the last week's date in the format: "2015-06-23"
i.e
"2015-06-16"
Js:
t = new Date(); // will give Tue Jun 23 2015 21:00:47 GMT-0700 (PDT)
t.toISOString(); // will give "2015-06-24T04:00:47.955Z"
the above date format i'm getting from the server. but I would like to get the last week date instead this week in the above format. How can i achieve that?
Thanks for the help in advance!
Im trying to get the last week's date in the format: "2015-06-23"
i.e
"2015-06-16"
Js:
t = new Date(); // will give Tue Jun 23 2015 21:00:47 GMT-0700 (PDT)
t.toISOString(); // will give "2015-06-24T04:00:47.955Z"
the above date format i'm getting from the server. but I would like to get the last week date instead this week in the above format. How can i achieve that?
Thanks for the help in advance!
Share Improve this question asked Jun 24, 2015 at 4:03 user1234user1234 3,1596 gold badges57 silver badges117 bronze badges 4- 1 momentjs. moment library is this something useful to you? – Shih-Min Lee Commented Jun 24, 2015 at 4:06
- 1 stackoverflow./a/3818198/507793 – Matthew Commented Jun 24, 2015 at 4:07
- This is javascript date + 7 days i hope you need it stackoverflow./questions/5741632/javascript-date-7-days – Cherryishappy Commented Jun 24, 2015 at 4:20
- I second moment.js. It saves a lot of brain cells. – Cymen Commented Jun 24, 2015 at 4:34
3 Answers
Reset to default 4- Create a
Date
. - Go back 7 days.
- Convert to string, grabbing only the date portion.
Like so:
t = new Date();
t.setDate(t.getDate() - 7);
var date = t.toISOString().split('T')[0];
You can use moment.js
It's simple.
var t = moment.subtract(7, 'd').format('YYYY-MM-DD');
//For example: 2015-06-16
You can use setDate() to get the previous date
t = new Date();
t.setDate(t.getDate() - 7);//this will get you the previous date
t.toISOString();
Now for formatting, you can think of a library like momentjs or you can do it manually
var formatted = t.getFullYear() + '-' + (t.getMonth() < 9 ? '0' : '') + (t.getMonth() + 1) + (t.getDate() < 10 ? '0' : '') + '-' + (t.getDate());
本文标签: javascriptGet a 7 days back date in quot20150623quot format using JqueryJsStack Overflow
版权声明:本文标题:javascript - Get a 7 days back date in "2015-06-23" format using JqueryJs - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745536134a2662279.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论