如何使用jquery得到上周的星期日日期
答
可以解析字符串,然后减去一天值,以获得一个星期日
var string = '01-31-2015';
var date = new Date(string);
date.setDate(date.getDate() - date.getDay())
snippet.log(date)
<!-- Provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
如果日期的简单解析不那么工作
var string = '01-31-2015';
var parts = string.split('-'),
date = new Date(parts[2], parts[0] - 1, parts[1]);
date.setDate(date.getDate() - date.getDay());
snippet.log(date)
<!-- Provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
答
var curr = new Date; // get current date
var first = curr.getDate() - curr.getDay(); // First day is the day of the month - the day of the week
var firstday = new Date(curr.setDate(first)).toUTCString();
firstday
"Sun, 18 OCT 2015 12:25:40 GMT"
答
获得最新周日一句话:
var latestSunday = new Date(new Date().setDate(new Date().getDate() - new Date().getDay()));
获得的最后一个星期日的一个句子:
var lastSunday = new Date(new Date().setDate(new Date().getDate() - (new Date().getDay()==0?7:new Date().getDay())));
似乎冗长,您可以根据需要划分的答案。那么,你可以使用其他日期格式插件来格式化日期,比如jQuery dateFormat
看一下momentJs – 2015-10-20 05:02:59