-1

I have a query that reads a date like this

var getstatusdate = $('#statusdate').val();

getstatusdate = "6/14/2016 12:00:00 AM"

Need to compare with another variable that stores date in the format below

var today = "06/14/2016"

How do I remove the time from getstatusdate and make it look like "06/14/2016"

I tried this but no luck.

var cleaneddate = getstatusdate.toDateString("MM/dd/yyyy")
Madhawa Priyashantha
  • 9,633
  • 7
  • 33
  • 60
Baba
  • 2,059
  • 8
  • 48
  • 81
  • Possibly this will answer your question: http://stackoverflow.com/questions/1531093/how-to-get-current-date-in-javascript – drew_w Jun 14 '16 at 15:37
  • Thanks. Am not trying to get a new date. I just need to format an existing date. – Baba Jun 14 '16 at 15:42
  • @user2320476 If you look at the accepted answer for that question drew_w posted, it shows you how to format the date – mhodges Jun 14 '16 at 15:43
  • Also, if you are not comparing equality, but rather < or > , you can simply use new Date(getstatusdate).getTime() < new Date(today).getTime() – mhodges Jun 14 '16 at 15:45

2 Answers2

0

In alternative, if you don't want to manage dates but manipulate strings, you can use a regex like this one;

var getstatusdate = $('#statusdate').val();
var myRegexp = ^(\d\d\/\d\d\/\d\d\d\d)(.*);
var match = myRegexp.exec(getstatusdate);

var cleaneddate = match[1];

match group 1 matches starting string (^) with: 2 digit \ 2 digit \ 4 digit

rest it's optional and ignored (=> in match[2])

MrPk
  • 2,862
  • 2
  • 20
  • 26
-1

If you are taking new Date() then like this

var dt = new Date();
var myDate = dt.getDate() + "/" +parseInt(dt.getMonth() + 1) + "/" + dt.getFullYear();
alert(dt); //Tue Jun 14 2016 21:12:07...
alert(myDate); //14/6/2016 

else based on your given date, you can use split() as follow

getstatusdate = "6/14/2016 12:00:00 AM"
alert(getstatusdate.split(" ")[0]); //6/14/2016
Hardik Pithva
  • 1,729
  • 1
  • 15
  • 31