Following piece of code is throwing an error: System.TypeException: Invalid date/time: 04/07/2016 02:00 PM

    if (t.activitydate != null)
        DateTime activityDate = t.activitydate;
        activityDate = activityDate.addHours(13);
TimeZone tz = UserInfo.getTimeZone();
        String dt =  activityDate.format('MM/dd/yyyy hh:mm a',tz.getId());
        myObj.Date_Time__c = DateTime.parse(dt); //this is a Date/Time object

This error happens only for users from Europe but not for users in North America. Could someone please help me point out what is the issue and fix?

  • Can you please tell in few words about what you are trying to achieve? – kurunve Apr 6 '16 at 16:01
  • just want to remove time from original datetime and set it to 1PM – user569925 Apr 6 '16 at 16:03
  • 4
    Then I would recommend you to do it like this: myObj.Date_Time__c = DateTime.newInstance(t.activityDate.year(),t.activityDate.month(),t.activityDate.day(),13,0,0); That would do it in user time zone, you can use methods with gmt to make them in gmt format. About why your code is failing -- datetime.parse try to parse date time string in current user locale, and in european one days goes before months – kurunve Apr 6 '16 at 16:15
  • 1
    please add this as an answer so I can mark it. will help community – user569925 Apr 6 '16 at 16:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.