How to get last week sunday date using jquery

462
November 22, 2016, at 3:30 PM

I am trying to get last week sunday date with "mm-dd-yyyy" format using jquery. Is there an easy solution out there for this?

Answer 1

You can parse the string then subtract the day value to get the previous sunday

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>

If the simple parsing of date is not working then

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>

Answer 2
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"
Answer 3

get latest sunday with one sentence:

var latestSunday = new Date(new Date().setDate(new Date().getDate() - new Date().getDay()));

get last sunday with one sentence:

var lastSunday = new Date(new Date().setDate(new Date().getDate() - (new Date().getDay()==0?7:new Date().getDay())));

it seems verbose, you can divide the answer as you wish. then, you can use other date format plugins to format the date, like jQuery dateFormat

Rent Charter Buses Company
READ ALSO
Toggle class of sticky menu on scroll with overflow hidden on page

Toggle class of sticky menu on scroll with overflow hidden on page

I want to add a class. custom-menu-bg to sticky menu.

446
hide and show content in loop php jquery

hide and show content in loop php jquery

Hi guys i need some help i have these code i need to show content by id[nnn]after clik to show content i need to show content by the same id.

373
statement in jQuery without semicolon

statement in jQuery without semicolon

I have a statement like below ,.

311
How to remove minimize and maximize property of a window by Javascript

How to remove minimize and maximize property of a window by Javascript

I want to remove minimize and maximize button from a window. So that the Popup window that appears in front should not be able to disappear from the front until it is closed.

368