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
Manish.SinghManish.Singh 

string to Datetime convertion

Hi Everyone,
I have used following code to convert datetime string to datetime but it not shows correct value. Please help me solve this.
string currentDateString=Datetime.Now().format('YYYY-MM-dd HH:mm:ss','Asia/Kolkata');
system.debug('currentDateString '+currentDateString);
datetime targetDate = Datetime.valueof(currentDateString);
system.debug('targetDate '+targetDate);
User-added image
My expectation both value will be same.
Please help.
Thank you
Best Answer chosen by Manish.Singh
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Manish,

Please use the below code.
 
string currentDateString=Datetime.Now().format('YYYY-MM-dd HH:mm:ss','Asia/Kolkata');
system.debug('currentDateString '+currentDateString);
datetime targetDate = datetime.valueOfGmt(currentDateString);
    system.debug('time'+targetDate);

If this solution helps, Please mark it as best answer.

Thanks,