0

I know there are a lot of such questions and i tried many of them, but i can't make it work properly, i'm always getting wrong time. I have table with value time, which is set to timestamp. The idea is to use it for forum post with date and time when this post were created, so everyone can see correct time in his own timezone, not servers. This value returns me string like this '2023-01-22 11:42:50', but the time is wrong. How to convert it to local time on client side? And without specifying timezone. I tried this:

const getTime = (str) => {
const d = new Date(str);
return `${d.toLocaleDateString()} ${d.toLocaleTimeString()}`;
}

This returns me '22.01.2023 11:42:50' which is how i wanted but the time is still wrong. Or maybe i should do it on server side? If so, then how to do it in PHP?

UPDATE: I just tried new Date('2023-01-22 11:05:08').toString() from another question and this gives me same time again. I made this post at 18:05 not 11. How to change it?

Nikos
  • 27
  • 5
  • What is the original "11:42:50"? UTC? – deceze Jan 22 '23 at 16:05
  • I'm not sure, but think yes. Is it utc by default in mysql? I didn't change anything, just created column time and selected timestamp there. – Nikos Jan 22 '23 at 17:17
  • `new Date` doesn't understand that your time string is supposed to represent UTC time. You need to make that unambiguous by using the standardised format `2023-01-22T11:05:08Z`. Then it can be properly localised. – deceze Jan 22 '23 at 18:31

0 Answers0