snippetMinor
Format current date
Viewed 0 times
formatcurrentdate
Problem
I was refactoring some code and found this:
And I changed it to
CurrentDateTime isn't used anywhere just in those four lines.
How a programmer can even do that? Or WHY?
DateTime CurrentDateTime = System.now();
Datetime ESTDate = Datetime.newInstance(CurrentDateTime.year(),CurrentDateTime.month(),CurrentDateTime.day(),CurrentDateTime.hour(),CurrentDateTime.minute(),CurrentDateTime.second());
String myDateFormat = ESTDate.format('yyyy-MM-dd hh:mm:ss');
String myDate = myDateFormat.replace(' ', 'T');
object1.birthDate = myDate;And I changed it to
object1.birthDate = datetime.now().format('yyyy-MM-dd\'T\'hh:mm:ss');CurrentDateTime isn't used anywhere just in those four lines.
How a programmer can even do that? Or WHY?
Solution
The programmer probably didn't understand how quoting works in
DateTime.format() - just couldn't get the 'T' to appear in the string, so bailed, put the space there and replaced it. Creating ESTDate from CurrentDateTime is particularly weird, though!Context
StackExchange Code Review Q#21595, answer score: 5
Revisions (0)
No revisions yet.