function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
rajesh k 10rajesh k 10 

Urgent:How to convert String(Tue Nov 18 00:00:00 GMT 2014) to Date(11/18/2014)?

Hi,
       I have Two Strings Like
String s1='Tue Nov 18 00:00:00 GMT 2014';
String s2='Sun Nov 30 00:00:00 GMT 2014';

How To get s1 and s2 are like below
o/p:s1=11/18/2014
      s2=11/130/2014

help me...


 
RamuRamu (Salesforce Developers) 
Follow the guidelines given in the solution below

https://developer.salesforce.com/forums/ForumsMain?id=906F00000008wAgIAI
rajesh k 10rajesh k 10
Hi Ramu ,

                   I tried this URL i didn't get output like  Date s1=11/18/2014

please help me....
Amit Chaudhary 8Amit Chaudhary 8

Datetime GMTDate = Datetime.newInstanceGmt(2011,6,1,12,1,5);
String strConvertedDate = GMTDate.format('MM/dd/yyyy HH:mm:ss', 'America/New_York');
System.assertEquals( '06/01/2011 08:01:05', strConvertedDate);

 
rajesh k 10rajesh k 10
Hi Amit Chaudhary ,
                                I tried but i got DateTime Not Exist Error ..

 
Amit Chaudhary 8Amit Chaudhary 8
Hi Rajesh,

Please convert your date in below format. Then try below code:- 

String s1='11/6/2014 12:00 AM';
DateTime dt = DateTime.parse(s1); 
String utcDt = dt.format('dd-MM-yyyy', 'UTC');
System.debug('----------->'+utcDt );

Thanks,
Amit Chaudhary
rajesh k 10rajesh k 10
Hi Amit,
             In debug log my date is 2014-11-20 00:00:00
            I an visualforce page visible like 'Tue Nov 20 00:00:00 GMT 2014';
NOTE:In visualforce page i was using Datepicker
I tries but i didn't get 2014-11-20 date
Amit Chaudhary 8Amit Chaudhary 8
Hi Rajesh,

Below code will resolve your issue :-

String myDate = 'Tue Nov 18 00:00:00 GMT 2014';
String strMnth    = myDate.substring(4,7);
String day        = myDate.substring(8,10);
String year       = myDate.substring(24,28);
string strMonth ;
if(strMnth=='Jan')
   strMonth ='1';
else if (strMnth == 'Nov' )
   strMonth ='11';

String strDate = strMonth +'/'+day+'/'+year;
System.debug('------>'+strDate );

Please add all else for all 12 mnths

Thanks
Amit Chaudhary
rajesh k 10rajesh k 10
Hi,
       Thank you for reply
Below String how to assighn Date field?
String strDate = strMonth +'/'+day+'/'+year;


My code:
=======
String myDate = cookiestartdate;                         //Sat Nov 22 00:00:00 GMT 2014
        String strMnth    = myDate.substring(4,7);
        String day        = myDate.substring(8,10);
        String year       = myDate.substring(24,28);
        string strMonth ;
        if(strMnth=='Jan')
           strMonth ='1';
        
        else if (strMnth == 'Nov' )
           strMonth ='11';
        
        Startdate = String.valueOf(strMonth +'/'+day+'/'+year);//Here Startdate  is the date picker field Datatype Date
        System.debug('------>'+Startdate);
                
 
rajesh k 10rajesh k 10
Hi Amit,
I tried below code

String myDate = cookiestartdate;
        String strMnth    = myDate.substring(4,7);
        String day        = myDate.substring(8,10);
        String year       = myDate.substring(24,28);
        string strMonth ;
        if(strMnth=='Jan')
           strMonth ='1';
        
        else if (strMnth == 'Nov' )
           strMonth ='11';
        
         String strDate = strMonth +'/'+day+'/'+year;
        
        Startdate  = date.parse(strDate);
        System.debug('------>'+Startdate);
                
        I get  Starting position out of bounds: 24 Error
 
Amit Chaudhary 8Amit Chaudhary 8
Hi Rajesh,

Please use below code :-

String myDate = 'Tue Nov 18 00:00:00 GMT 2014';
String strMnth    = myDate.substring(4,7);
String day        = myDate.substring(8,10);
String year       = myDate.substring(24,28);
string strMonth ;
if(strMnth=='Jan')
   strMonth ='1';
else if (strMnth == 'Nov' )
   strMonth ='11';

String strDate = strMonth +'/'+day+'/'+year;
System.debug('------>'+strDate );
date mydate1 = date.parse(strDate);
System.debug('------>'+mydate1 );



PS: if this answers your question then hit Like and mark it as solution!

 
Amit Chaudhary 8Amit Chaudhary 8
Please check your "cookiestartdate" valiable format and update index in myDate.Substring(24,28) according to format.
Well above code is working fine for me:-

User-added image
 
rajesh k 10rajesh k 10
Hi,
         I also get like your debug only .
When i refresh page on that time debuglog Startdate is--------------------------> 2014-11-22 00:00:00
                                                 In an visualforce field location is------------>  Sat Nov 22 00:00:00 GMT 2014  

Based on which i will assign startdate inputText value(Datepicker)?   
 
Amit Chaudhary 8Amit Chaudhary 8
Hi Rajesh,
Please send me your Apex and VF page code. So that i check.
Thanks
Amit Chaudhary
shailendra singhaishailendra singhai
 Datetime thisDT =ffrec.Order_Create_Date__c;    //it was 2019-01-01 00:00:00
 string orderdate= thisDT.format('MM/dd/yyyy','GMT');   //output of this was 01/01/2019
when you format the datetime , it convert with userlocal time. so you need to change converted format in GMT.