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
Sebastian PageSebastian Page 

How to convert createdDateTime value Into Timevalue

Hi All,

I have a requirment in my project. I need convert CreatedDate value Into Custom field  Timevalue using apex . I want only Time value not date. And Time values should be in 24 hour format such like that 14:00 of 2:00 PM . If anyone use these type functionality in your project please guide me.

Thanks & Regards
Sebastian Page 
CharuDuttCharuDutt
Hii Sebastian Page
You can Do This By Formula Field Data Type Time
Formula
TIMEVALUE(CreatedDate +(330/1440))
Please Mark it As Best Answer if it helps
Thank You!
 
Malika Pathak 9Malika Pathak 9
Hi Sebastian Page,
    
Greetings!

If you want to store time into Time field, then use this code
 
DateTime createdDate = [SELECT CreatedDate FROM Account LIMIT 1].CreatedDate;
Time timeObj = createdDate.time();
System.debud(timeObj);

If you want to use the in-text(String) field, then use this code
 
DateTime createdDate = [SELECT CreatedDate FROM Account LIMIT 1].CreatedDate;
Time timeObj = createdDate.time();

String timeObj1 = createdDate.hour()+':'+createdDate.minute()+':'+createdDate.second();    //with seconds
String timeObj2 = createdDate.hour()+':'+createdDate.minute();    //without seconds
System.debug(timeObj1);
System.debug(timeObj2);


Please mark it as the best answer if it helps you to fix the issue.

Thank you!

Regards,
Malika Pathak