I'm sending a date value from my lwc to apex. the date value comes from a lightning-input type date, so the format is year-month-day (for example, 2021-06-01). In apex, this is how I process it, very simple:

public void import(Object value) {
     this.value = Date.valueOf(value);

Like this, apex throws "invalid date: year-month-day" I tried converting it to ISO string in js, so getting something like "2021-06-01T00:00:00.000Z" and apex also throws an invalid date error!! So which format does it accept? day/month/year? month/day/year? I know it has been asked a lot but I couldn't find a specific answer.

EDIT: I even tested in anonymous execute console, and tried doing Date.valueOf('2021-06-01') and it let me! so how come it works there and not in my code? Does it have something to do that I'm receiving an Object as argument instead of a String?

1 Answer 1


As your method param is an Object and not string, that is why Date.valueOf method is not appending 00:00:00 by itslef and giving error. Convert/ typecast Object into string and it should start working.

public void import(Object value) {
     String val = String.valueOf(value); //String val = (String) value;
     this.value = Date.valueOf(val);

If you check documentation of Date class, Date.valueOf function accepts a string and the string format must be, although this function automatically appends time as 00:00:00 if you dont provide any time values.

(2021-06-01 08:11:00)
 String stringDate = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;
 Date myDate = date.valueOf(stringDate);

one other way to get the date value is to create a new instance using year, month and day. If you are passing this string - 2021-06-01 as value from LWC, then you can convert it into a list of string first and pass it as integer values to newInstance method:

String dateValue = '2021-06-01';
List<String> token = dateValue .split('-');
//(Date.newInstance(year, month, day))
Date dt = Date.newInstance(Integer.valueOf(token[0]),Integer.valueOf(token[1]),Integer.valueOf(token[2]));
  • thanks! I'll try this. You know, I actually checked the documentation (i didn't want to ask in here before doing that), but then I actually tried in execute anonymous, and Date.valueOf() with the year-month-day format worked. Do you have any idea why it worked there and not in my code? Maybe in execute anonymous, there is no local time/ time zone problems there, and only in real code? Jun 1, 2021 at 15:00
  • Actually its not the problem of locale/time zone. You are passing value as an object. if you convert that into a string, it will work in apex as well. Let me update one more solution above to quick fix this Jun 1, 2021 at 15:18

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.