Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how can i convert string type of data into date using java script or jquery, i searched from lots of websites, but all are not working properly, i have written like as follows:

var actualdate = $(this).find('td').eq(0).text().trim();//coming date in the form of 12-sep-2014[all dates from database]

var parts = actualdate.split('-');
var converteddate = new Date(parts[2],parts[1],parts[0]);

if i display the value of converteddate in alert, it is showing "invalid date!!!"

and after conversion i should identify number of days between that converted date and todays date, so that if those days are crossed 30 days from todays date i need to display some alert msg, please help me to solve this,
Posted

JavaScript
function customParse(str) {
  var months = ['Jan','Feb','Mar','Apr','May','Jun',
                'Jul','Aug','Sep','Oct','Nov','Dec'],
      n = months.length, re = /(\d{2})-([a-z]{3})-(\d{4})/i, matches;

  while(n--) { months[months[n]]=n; } // map month names to their index :)

  matches = str.match(re); // extract date parts from string

  return new Date(matches[3], months[matches[2]], matches[1]);
}


This function will return the actual date from your date string and then you can compare two dates

JavaScript
customParse("19-Aug-2010") > customParse("18-Aug-2010");
 
Share this answer
 
Comments
pavan_ kumar 24-Sep-14 2:45am    
thanks, it is working
JavaScript
/********* Your code *****************/
var parts = actualdate.split('-');
var converteddate = new Date(parts[2],parts[1],parts[0]);
 
//if i display the value of converteddate in alert, it is showing "invalid date!!!"

/*======Solution:
I think you are pass invalid arguments in above mention code other wise if you pass valid data it should work and working i have test.
*/
var str = "05/06/2014";
var dt =new Date(str);
//you can use this to get separate date month and year. 
dt.getDate()+"-"+(dt.getMonth()+1)+"-"+dt.getFullYear();


if any issue then let me know.

-> M.U
 
Share this answer
 
Comments
pavan_ kumar 25-Sep-14 1:04am    
thanks, i am passing month in the form of letter, thanks for remind me
MuhammadUSman1 25-Sep-14 1:18am    
You welcome

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900