Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

lilo's avatar
Level 2

How to get selected day name in date picker?

Hi,

I want to display the day name of the selected date. Now, I can choose date via date picker like this: 13.10.2020 in my panel but I want to display like this: 13.10.2020 Tuesday

How to do this?

0 likes
9 replies
Kinomej's avatar

Are you using a framework?

A javascript only way could be:

const today = new Date()

const dateString = [today.getDate(), today.getMonth() + 1, today.getFullYear()].join('.');

const weekday = today.getDay();

const weekdays = [
  'Sunday',
  'Monday',
  'Tuesday',
  'Wednesday',
  'Thursday',
  'Friday',
  'Saturday'
]

console.log(dateString + ' ' + weekdays[weekday])

with momentjs its even easier

const today = moment()

console.log(today.format('D.M.Y dddd'))
lilo's avatar
Level 2

Hi @kinomej I'm using laravel. So where do I put this in controller or in blade?

teos_97's avatar

@lilo You could use Carbon and format it something like :

Carbon::parse($yourdate)->format('d.m.Y l');

Not tested but should output something like 13.10.2020 weekday

lilo's avatar
Level 2

Hi @teos_97 thanks for the answer, I did like this

    $date = $eventReport->date;
        $eventReport->date = Carbon::parse($date)->format('l');

But I get this error:

Carbon\Exceptions\InvalidFormatException A two digit day could not be found Data missing

What could be missing?

teos_97's avatar

@lilo could you show me what your eventReport->date looks like?

a4ashraf's avatar

@lilo

what is output of your eventReport->date variable

it must be date e.g 2020-10-10

try

use Carbon\Carbon;


Carbon::parse('15-10-2020')->format('d.m.Y l');

// output 

15.10.2020 Thursday
lilo's avatar
Level 2

@a4ashraf

I tried like this:

$date = $eventReport->date;
$day = Carbon::parse($date)->format('d.m.Y l');

The output of dd($day) gives "15.10.2020 Thursday" but when I try this:

$eventReport->date = Carbon::parse($date)->format('d.m.Y l');

I get this error:

Carbon\Exceptions\InvalidFormatException Unexpected data found. Unexpected data found. Trailing data

How can I equalize the $event_report->date to $day?

The format of $date is 'Y-m-d H:i:s'. (I tried to convert the format to 'Y-m-d l' still gives the same error)

a4ashraf's avatar

@lilo

are you comparing two dates or you need to save the date in your date field?

first, if you are saving the date filed in DB

that is not possible because your date filed is DateTime type and you are saving it with string (10.10.2020 Monday)

I suggest you that save the date in your date fields as it (2020-10-10 11:23:23)

when you are accessing this date field create an accessor

public function getFullNameAttribute($value)
{
    	return Carbon::parse($value)->format('d.m.Y l');
}

Please or to participate in this conversation.