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
TryCodingTryCoding 

How to extract time for local time zone

Hi All

I want to extract time for my local time zone which i have set in my personal information

 

like  consider apex code below:

 

DateTime cDT = system.Now();

String localDateTime = cDT.format();

 

now localDateTime will be like 4/1/2011 4:22 PM

 

I want to extract 4:22 PM out of this

 

One method given in salesforce documentation is hour,minute methods. But this method takes datetime as argument. But our format method returnes a string.

 

The solution may be convert the string returned by format method to datetime type.

But could not. i tried parse method. it converted my local time back to GMT Time. Secondly i tried valueOf Method. But it accepts string in a specific format. 

 

Please help me solving this problem. I have tries from my side. searched on net. but got no solution

 

 

Best Answer chosen by Admin (Salesforce Developers) 
dotnet developedotnet develope

Hi Friend,

 

Please find the below solution

 

DateTime cDT = system.Now();
String localDateTime1 = cDT.format('h:mm a');

 

Thanks & Regards

:smileyvery-happy:

All Answers

dotnet developedotnet develope

Hi Friend,

 

Please find the below solution

 

DateTime cDT = system.Now();
String localDateTime1 = cDT.format('h:mm a');

 

Thanks & Regards

:smileyvery-happy:

This was selected as the best answer
TryCodingTryCoding

Thanks buddy.. This works..  A lot of thanks

 

i tried the same for extracting date too

Just for the help of others i am posting this here

date can be extracted via same method

 

Datetime cDT = System.now();

String currentDate = cDT.format('d:M:yyyy ');