+ Start a Discussion
ahmadkhanahmadkhan 

NEED HELP WITH STRING TO DATE Conversion

Hi guys,

 

i need an urgent help with this

 

i have two picklist one Months in which there are twelve month {January febuarary,..so on} and another is years{2013,2014, to 2020}

i need to convert these two picklists to one date field can any one who wrote this function and share his code? i am in a lil bit hurry.

 

Thanks,

Ahmed

Best Answer chosen by Admin (Salesforce Developers) 
AsitM9AsitM9

map the months in page to corresponding numbers ie Jan->1,feb->...

and  for year 2016->2016 then for date 1->1,2->2...so on.(all this you have to do in a VF page).

 

Then in controller

String year{get;set;}
String date1{get;set;}
String mnth{get;set;}

Date mydate=date.newInstance(Integer.valueOf(year),Integer.valueOf(mnth),Integer.valueOf(date1));

 

All Answers

AsitM9AsitM9

map the months in page to corresponding numbers ie Jan->1,feb->...

and  for year 2016->2016 then for date 1->1,2->2...so on.(all this you have to do in a VF page).

 

Then in controller

String year{get;set;}
String date1{get;set;}
String mnth{get;set;}

Date mydate=date.newInstance(Integer.valueOf(year),Integer.valueOf(mnth),Integer.valueOf(date1));

 

This was selected as the best answer
MellowRenMellowRen

ahmadkhan

 

You didn't mention if you needed this code in a trigger or a controller/extension. I am going to assume a trigger and also assume you do not have a field for the day of the month (and you just want the 1st of the month) then you can build a map for the Months and convert as such:

 

        Map<String, Integer> mthMap = new Map<String, Integer> ();
        mthMap.put('January',1);
        mthMap.put('February',2);
        mthMap.put('March',3);
        //etc, etc
        
        the_date_field__c = Date.newinstance(Integer.valueOf(the_year_field__c), mthMap.get(the_month_Field__c), 1);

Hope this helps.

 

Regards

MellowRen

ahmadkhanahmadkhan

Thanks Alot.