0

My string format : 10/11/2016 01:00 AM

I want to convert this String to DateTime to support in every user's locale format

I tried Date.parse, Date.valueOf but am not getting expected value.

Any help is appreciable thanks!.

  • Assuming you mean that the format is fixed (and does not match the user's local settings), the bad news is that there isn't a built in method to help with the parsing and you'll have to write your own code. See this example. – Keith C Oct 11 '16 at 11:23
  • the format is MM/dd/yyyy with AM and PM whatever will be. So this is supporting for united states and indian format but it is not supporting for singapore and some other countries due to 24-hour format – Prince Oct 11 '16 at 11:54
3

You can format with userinfo.getTimeZone();

DateTime dtNow = //any date here 

String formattedDate = dtNow .format('yyyy-MM-dd',  UserInfo.getTimeZone().toString());

Updates

Try below code snippet to convert the string to DateTime.

String val = '2015-12-01T12:44:00.000+0000'; 

DateTime date1 = (DateTime)Json.deserialize('"'+val+'"', DateTime.class);

System.debug(date1)  ---> 2015-12-01 12:44:00

String dateformat = 'yyyy-MM-dd HH:mm:ss:sssZ';
String abc = date1.format(dateformat,''+userinfo.getTimeZone().toString());
system.debug(abc);
  • I gave like this, DateTime dtNow = DateTime.parse(obj.stringfield); String formattedDate = dtNow .format('yyyy-MM-dd', userinfo.getTimeZone()); but getting error in 2nd line, Method does not exist or incorrect signature: [Datetime].format(String, TimeZone) – Prince Oct 11 '16 at 11:55
  • @Prince ahh this is UserInfo.getTimeZone().toString().. updated answer pls check – Ratan Paul Oct 11 '16 at 12:07
  • My input from visualforce page is **10/11/2016 02:30 PM **. In the apex code am converting this to DateTime. When the user is from some other place with the format **11/11/2016 14:30 getting error while clicking the save button in visualforce page saying that 'Invalid Date/Time'. ** – Prince Oct 11 '16 at 12:19
  • So when I pass the string input to convert DateTime it is getting error in some countries. I am parsing string to DateTime convertToDateTime = DateTime.parse(input); like this. – Prince Oct 11 '16 at 12:21
  • @Prince issue with parsing and timmezone both ? – Ratan Paul Oct 11 '16 at 12:26
0

Take a look at following link, hope it help. Date/Time Field - Independent of User Locale

  • I think the question in link is different from Mine. In the link, the user dont want to change the date and time regardless of the locale. But I want to convert this according to user locale. – Prince Oct 11 '16 at 11:05
  • Hi @Prince, I've try the recommended by Ratan. That should be match your question. "String timeTxt = '20:48 11/10/2016'; DateTime dt = DateTime.parse(timeTxt); System.debug('Time: '+ dt.format('MM/dd/yyyy HH:mm:ss')); TimeZone tz = UserInfo.getTimeZone(); System.debug('Time zone 2: '+ dt.format('MM/dd/yyyy HH:mm:ss', 'America/New_York'));" – ted Oct 11 '16 at 11:57

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.