2015-10-20 88 views

回答

0

可以解析字符串,然後減去一天值,以獲得一個星期日

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>

0
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" 
0

獲得最新週日一句話:

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

相關問題