0

I'm trying to form a Javascript date that looks like "2015-12-31 07:12:49", and is using Vancouver time.

I've gone through these forums and tried a million different functions but can't successfully build the date.

I tried following this article, but it doesn't show how to format the date to MYSQL datetime. How to get Time of specific timezone using javascript?

Any help would be greatly appreciated!

Community
  • 1
  • 1
steeped
  • 2,613
  • 5
  • 27
  • 43
  • 1
    Why not just get the dateTime, and the parse it and rebuild it? – Jacques ジャック Dec 31 '15 at 18:23
  • @Jacques, forgot to mention I'm a PHP guy and not Javascript... sorry! Can you send me an example of what you mean? – steeped Dec 31 '15 at 18:24
  • This is how I got the datetime: (new Date()).toISOString().substring(0, 19).replace('T', ' ') -- not sure what timezone that is though, and if it's different for every browser. – steeped Dec 31 '15 at 18:25
  • Do you already have the date, or are you generating one? – Jacques ジャック Dec 31 '15 at 18:25
  • @Jacques this is how I'm generating it: (new Date()).toISOString().substring(0, 19).replace('T', ' '). Wrong timezone though... – steeped Dec 31 '15 at 18:26
  • How would you do it in PHP? How about doing it in PHP and then grab it via ajax? – PeterKA Dec 31 '15 at 18:29
  • @jValdron I went through that post and tried it out, but I just can't figure out how to format it like MySQL datetime. – steeped Dec 31 '15 at 18:32
  • @steeped Personally I've stopped using native JS for times, I've always used moment.js (http://momentjs.com/). They have a neat library to parse and convert date/time to all kinds of formats. If you know your date/time will always be -8, you could just create a date object from your timetsamp like so (http://stackoverflow.com/questions/3075577/convert-mysql-datetime-stamp-into-javascripts-date-format) and then do * -8 hours. – jValdron Dec 31 '15 at 18:36
  • The answer I marked as duplicate explains why you cannot do what you are asking. Specifically, the `Date` object itself does not have a format, nor can it be put into any particular time zone. You can craft a string using moment.js (or manually), and if you need to convert to a particular time zone first, then you can use moment-timezone or one of the other options mentioned in the duplicate. – Matt Johnson-Pint Dec 31 '15 at 19:20

0 Answers0