0

In my script tag, var today shows in the Sat Sep 07 2019 00:00:00 GMT+0530 (India Standard Time) date format, and I want to change this date format to the yyyy-mm-dd format.

My code:

<script>
    $(document).ready(function() {
        var date  = new Date();
        var today = new Date(date.getFullYear(), date.getMonth(), date.getDate());
        console.log(today);
    });
</script>

I am trying like this:

<script>
    $(document).ready(function() {
        var date  = new Date();
        var today = new Date(date.getFullYear(), date.getMonth(), date.getDate());
        var newDate = "<?php echo date("Y-m-d", strtotime(today)); ?>";
        console.log(newDate);
    });
</script>

How can I change date format using strtotime()?

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
farhantechno
  • 575
  • 2
  • 11
  • 1
    https://stackoverflow.com/questions/23593052/format-javascript-date-to-yyyy-mm-dd – Manzolo Sep 07 '19 at 12:03
  • 1
    You should probably have a look at the difference between code that is run on the server (like PHP) and on the client (like JavaScript) and how they can interact with each other (hint: not like you think) – Andreas Sep 07 '19 at 12:05
  • Why are you passing a javascript variable to PHP? – Cue Sep 07 '19 at 12:06
  • 1
    @Andrea Manzi: There is for 110% sure a duplicate somewhere, but isn't that one about parsing a string, rather than just formatting (misleading title)? – Peter Mortensen Sep 07 '19 at 12:48
  • @PeterMortensen Yes, but the accepted answer uses the `Date` object to format the value. Using that exact code (but skipping `var d = new Date(date)` to parse the string since OP here already has `today` as a Date object and can just use `today` where the original code uses `d`) will provide the expected result. – rickdenhaan Sep 07 '19 at 13:12
  • 1
    As for *"How can I change date format using `strtotime()`?"* -- you don't. `today` is a Javascript variable, PHP has no access to it. See [this question](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) for a more detailed explanation. – rickdenhaan Sep 07 '19 at 13:14

6 Answers6

4

With PHP get date of today and change format


$origDate = "2019-01-15";

$newDate = date("m-d-Y", strtotime($origDate));
echo $newDate;

Output

01-15-2019

With js

  var date  = new Date();
  var year=date.getFullYear();
  var month= date.getMonth();
  var date=date.getDay();

 console.log(` ${day}:${month}:${year}`);

dılo sürücü
  • 1,989
  • 1
  • 12
  • 19
2

Use:

<script>
    $(document).ready(function() {
        var date  = new Date();
        var today = new Date(date.getFullYear(), date.getMonth(), date.getDate());
        var newDate = "<?php echo date("Y-m-d"); ?>";
        console.log(newDate);
    });
</script>
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
  • If you're setting `newDate` with PHP then `date` and `today` are completely redundant. – Cue Sep 07 '19 at 12:16
2

Don't use server-side logic when you already have everything to get the desired formatting:

<script>
    $(document).ready(function() {
        let today = new Date();
        let strToday = today.getFullYear() + "-" + today.getMonth() + "-" + today.getDay();
        console.log(strToday);
    });
</script>
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Zim
  • 1,327
  • 1
  • 9
  • 20
2

Simply format the date. Don't forget that getMonth() is zero-based.

const date = new Date();

const mm = date.getMonth() + 1;
const dd = date.getDate();

const format = [
  date.getFullYear(),
  (mm > 9 ? '' : '0') + mm,
  (dd > 9 ? '' : '0') + dd
].join('-');

console.log(format);
Cue
  • 2,489
  • 1
  • 10
  • 12
2

Write a function to change the date format in “yyyy-mm-dd”.

Demo on JSFiddle: http://jsfiddle.net/ecv7waru/

<script>
    $(document).ready(function() {
        var date  = new Date();
        var today = new Date(date.getFullYear(), date.getMonth(), date.getDate());
        var newDate = formatDate(today);
        console.log(newDate);
    });

    function formatDate(date) {
        var d = new Date(date),
                month = '' + (d.getMonth() + 1),
                day = '' + d.getDate(),
                year = d.getFullYear();

        if (month.length < 2)
            month = '0' + month;
        if (day.length < 2)
            day = '0' + day;

        return [year, month, day].join('-');
    }
</script>
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Lets-c-codeigniter
  • 555
  • 2
  • 4
  • 17
2

the Best with fewer line of code can be done by using moment as follows in ONE LINE

 var today =moment().format('YYYY-MM-DD');
 console.log(today)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.js"></script>
Nijin P J
  • 1,048
  • 1
  • 5
  • 12