devkon98's avatar

How to format date value to another format JS

Hello i have this JS code that gets date in format of d-m-Y but i need to format it to m-d-Y

var startDate = document.getElementById('startDateOutlay');

result: 2023-03-04

how it should be: 2023-04-03
0 likes
7 replies
Snapey's avatar

You sure? Do you want to rephrase your question and we can go again?

devkon98's avatar

@Snapey i have this code:

 <div class="text-center m-2">
                <label for="startdateOutlay">Start Date</label>
                <input type="date" name="startdateOutlay" id="startDateOutlay" value="{{date("Y-m-d", strtotime('monday this week'))}}"/>
                <label for="enddateOutlay">End Date</label>
                <input type="date" name="enddateOutlay" id="endDateOutlay" value="{{date("Y-m-d", strtotime('sunday this week'))}}"/>
                <button type="submit" id="outlayButton" onclick="getOutlayData()">Shfaq vlerat</button>
            </div>

in firefox the date shows 03/04/2023 on chrome shows 04/03/2023 how can i make it to have the same format dd-mm-yyyy in both firefox and chrome, if i change the Y-m-d to Y-d-m it will change in chrome to 03/04/2023 but again thats month day year so it will take me to march and not april.

Snapey's avatar

@devkon98 Look at the format of the date, and then look at what you say you want.

You are not explaining what you want.....

date shows 03/04/23 but this is one of d/m/Y or m/d/Y but I can't tell which

but your question you say you are changing to Y-m-d but give no examples of that

Snapey's avatar

You are also showing blade PHP code and then taking about JS

devkon98's avatar

@Snapey copy the code i just sent and run it on both chrome and firefox, you will see that on firefox it will show

03/04/2023 and on chrome will show 04/03/2023 what i want is for both chrome and firefox to have format 03/04/2023 basically Day/Month/Year not Month/Day/Year

devkon98's avatar

@Snapey the JS is wrong because it wont work anyways thats why posted the blade

Snapey's avatar

The date format you need to pass into a Date input value is always in the format 'YYYY-MM-DD'

You can create this format in PHP echo statements with 'Y-m-d'

How it is displayed in the browser depends on the locale of the browser and what language it thinks the user has. You cannot present the date differently to the date input and have it display differently.

Check the browser version compatibility for the date input;

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/date

Please or to participate in this conversation.