2

I have data type datetime in MySQL. It show value like this in phpmyadmin.

2023-04-21 09:51:47.539966

I use Django read datetime value from MySQL. I try to convert datetime value from MySQL to Javascript with toDateString() like this question.

<p id="demo"></p>

<script>
        var utc = new Date();
        var offset = utc.getTimezoneOffset();
        var t = '{{ data_log.updated }}'
        var d = new Date(t.toDateString());

        document.getElementById("demo").innerHTML = d;
</script>

The output not show anything. How to convert datetime from MySQL to datetime in Javascript?

user572575
  • 1,009
  • 3
  • 25
  • 45
  • 1
    Have you checked your console for errors? If t is the value you presented above, then **t.toDateString()** would produce an error as t is a string and not a date object. Change it to **new Date(t).toDateString();** so you get the date string of the date object – imvain2 Apr 21 '23 at 15:43

1 Answers1

0

you can try this :

const dateFromMysql = "2023-04-21 09:51:47.539966";

// conver the string into javascript date object
const dateObject = new Date(dateFromMysql);

// output complete date in javascript
document.getElementById("demo").innerHTML = dateObject;

// Fri Apr 21 2023 09:51:47 GMT+0000 (GMT)

and then you can chain all date methods that you want, in order to format date

dateObject.getFullYear();   //Get the year as a four digit number (yyyy)
dateObject.getMonth();  //Get the month as a number (0-11)
dateObject.getDate();   //Get the day as a number (1-31)
dateObject.getHours();  //Get the hour (0-23)
dateObject.getMinutes();    //Get the minute (0-59)
dateObject.getSeconds();     //Get the second (0-59)
dateObject.getMilliseconds()    //Get the millisecond (0-999)
dateObject.getTime();   //Get the time (milliseconds since January 1, 1970)
dateObject.getDay();  //Get the weekday as a number (0-6)
dateObject.Date.now();  //Get the time. ECMAScript 5.
dateObject.setDate()    //Set the day as a number (1-31)
dateObject.setFullYear()    //Set the year (optionally month and day)
dateObject.setHours()   //Set the hour (0-23)
dateObject.setMilliseconds()    //Set the milliseconds (0-999)
dateObject.setMinutes() //Set the minutes (0-59)
dateObject.setMonth()   //Set the month (0-11)
dateObject.setSeconds() //Set the seconds (0-59)
dateObject.setTime()    //Set the time (milliseconds since January 1, 1970)

to get an overview visite this : https://www.w3schools.com/js/js_date_methods.asp

for more details : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

  • Thank you. The output show Invalid Date. It have no error if I remove time like this "2023-04-21" but this will not display time. – user572575 Apr 21 '23 at 16:07
  • yes, you can test now – oussama ennadafy Apr 21 '23 at 16:12
  • your welcome that's my first time helping someone on stackoverflow, i'm happy that i solve your problem – oussama ennadafy Apr 21 '23 at 16:15
  • A good answer should explain why the OP has their issue and how your code fixes it. The explanations of *Date* *set* methods are wrong, the only limit on the values passed to *set* methods is that they produce a time value within the limits specified in [ECMA-262](https://262.ecma-international.org/#sec-time-values-and-time-range) (1e8 days either side of 1 Jan 1970). `date.setHours(1e4)` is fine, as is `date.setMonth(2343)`. – RobG Apr 24 '23 at 09:25