-1

I have the below code. If we try to retrieve the day, month and year in UK server setup. The date object returns an incorrect value.

var startDate = "30/08/2013"; 
var d = new Date(startDate); 
alert(d.getFullYear()); //2015

Please help me

OdinX
  • 4,135
  • 1
  • 24
  • 33
  • 1
    you forgot the code ;) – Toni Michel Caubet Sep 02 '13 at 11:03
  • var startDate="30/08/2013"; var d = new Date(startDate); alert(d.getFullYear());//2015 – user2739544 Sep 02 '13 at 11:04
  • Please update the code in the question i cannot do it.... – user2739544 Sep 02 '13 at 11:10
  • Give me any answer. why the uk server setup the date time year is increased while date is "30/08/2013" to something like "../../2015" – user2739544 Sep 02 '13 at 11:12
  • 2
    Use `var startDate = "08/30/2013";` ~ "months/days/years". Else with "30/08/2013" you set 30months + 8 days+ 2013years = June 8th 2015. Or use this additional javascript: `var startDate = "30/08/2013"; var s = startDate.split('/'); startDate = s[1]+'/'+s[0]+'/'+s[2]; alert(d); //2013` – Stano Sep 02 '13 at 11:35
  • Interestingly for me (in chrome) the code in the question alerts "NaN" (http://jsfiddle.net/dYuvS/) – Chris Sep 02 '13 at 11:37
  • thanks, I need to write convert date time code dynamic format because, Site is running uk(30/08/2013),india(30-08-2013),US(08/30/2013) etc.,. I would like to globalized US format. Please help me... – user2739544 Sep 02 '13 at 12:46
  • One thing to note: The Server has no impact on the result of the Date() object as client-side code... unless you're developing on Node.js. Date() is inherently US format dates. Date() is reliant on your system clock. If your system clock says 2015, your Date() says 2015. – simey.me Sep 02 '13 at 17:17

2 Answers2

1

Use moment.js if you want better control of parsing:

var startDate = "30/08/2013"; 
var m = moment(startDate,"DD/MM/YYYY");
alert(m.year()); //2013
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
0

Try passing the date as a list of arguments instead:

var startDate = "30/08/2013"; // Would also work with 30-08-2013
var startDateArray;

if(startDate.contains('/'))        
    startDateArray = startDate.split('/');
else if (startDate.contains('-'))
    startDateArray = startDate.split('-');
else if (startDate.contains('.'))
    startDateArray = startDate.split('.');

var d = new Date(startDateArray[2], startDateArray[1]-1, startDateArray[0]); // Month is 0 based, that's why we are subtracting 1
OdinX
  • 4,135
  • 1
  • 24
  • 33