I have to populate the Current User Time-Zone DateTime in a DateTime field. I am trying this :

String str = datetime.now().format();
DateTime d = Datetime.valueOf(datetime.now().format());
DateTime dtm =  (DateTime) DateTime.parse(str);
system.debug('datetime.now().format() = ' + str);
system.debug('Datetime.valueOf(datetime.now().format()) = ' + d);
system.debug('DateTime.parse(str) = ' + dtm);

But the problem is that after using format() it is giving the String format and converting it into DateTime using valueOf() & parse() giving the PST time which is the ORG Timezone, whereas I want the Current User time-zone DateTime. How can I get the CurrentUser Time-Zone DateTime in DateTime Data Type?

  • The question is ambiguous. Please update the code. What is 'str'? – Krishnan Mishra Sep 6 '20 at 18:11
  • I have edited the code. – Swapnil Jain Sep 6 '20 at 18:18
  • no, actually it was small edit, to mention 'str' variable. I am still stuck in this. – Swapnil Jain Sep 6 '20 at 18:57
  • 1
    I was able to solve the same issue with solution from link shared by David. – Vishal Sep 6 '20 at 20:30

Browse other questions tagged or ask your own question.