-3

I have Date object and would like it formated.

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

This is the format I would like to be able to print

yyyy-mm-dd hh:mm:ss

2017-08-09 19:48:54

Whatever I try I can't get it to using console.log to print that. I allways get something like

Wed Aug 09 2017 19:48:54 GMT+0200 (Central Europe Daylight Time)
or
Wed, 09 Aug 2017 17:48:54 GMT

I do not need all that extra information embedded. If I create Date object and time is 19:53:00 and date is 2017-08-09 then I need just this 2017-08-98 19:53:00

How do I get that exactly? I need to send this to server just like that.

Community
  • 1
  • 1
niko craft
  • 2,893
  • 5
  • 38
  • 67

3 Answers3

2

This works fine :

function T(n) { return n<10?'0'+n:n; }
var d = new Date();
var formatted = d.getFullYear()+'-'+T(d.getMonth())+'-'+T(d.getDay())+' '+T(d.getHours())+':'+T(d.getMinutes())+':'+T(d.getSeconds());
console.log(formatted); // => 2017-07-03 19:08:30
DjaouadNM
  • 22,013
  • 4
  • 33
  • 55
1
let d = new Date();  // prints Wed Aug 09 2017 13:55:59 GMT-0400
d.toISOString();     // prints "2017-08-09T17:55:59.428Z"
// process the string to output in your desired format
c2huc2hu
  • 2,447
  • 17
  • 26
  • 1
    Good answer, but it's worth checking for obvious duplicates first and just flagging the question instead. – GKFX Aug 09 '17 at 18:04
  • 1
    its not a problem to get 2017-08-09T17:55:59.428Z but as you see that is 4 hours forward in time, I want 2017-08-09 13:55:59 – niko craft Aug 09 '17 at 18:08
  • @maxit I'm in GMT-4 and ISO8601 is always in GMT, so that's the result for me. To handle timezones, you'll need something more complicated like moment.js. – c2huc2hu Aug 09 '17 at 18:32
  • hi I am using moment, however I have on server implemented handling of timezones and I allow user of my application to pick the timezone he is in. So that is why I need to send to the server the simple time format of what user selected in the datetime picker component that is written in Vue, this component gives me this kind of format: 2017-08-09T17:55:59.428Z but that is problem since server takes that and applies its calculations and time ends up being wrong. Perhaps I should do all time zone calucations on client and not server? What is better approach from your experience? – niko craft Aug 09 '17 at 19:11
  • I am using php on backend and I used this library https://github.com/camroncade/timezone to convert times to correct timezones when saving the time I do something like Timezone::convertToUTC($published_at, Auth::user()->timezone); and when reading from db Timezone::convertFromUTC($this->attributes['published_at'], $timezone, 'Y-m-d H:i'). This works fine for me. However would doing this on client be better choice or not? – niko craft Aug 09 '17 at 19:16
  • I've never used PHP, so I'm not sure where it would be easier. If you have it implemented already, you might as well keep it though – c2huc2hu Aug 09 '17 at 19:20
0

You could use momentjs library:

> moment().format('YYYY-MM-DD HH:mm:ss')
< "2017-08-09 01:57:51"

You can also use moment with a javascript Date object:

const myDate = new Date()
moment(myDate).format('YYYY-MM-DD HH:mm:ss')
Ulysse BN
  • 10,116
  • 7
  • 54
  • 82
  • 1
    Advising the use of an external library when something is available as standard isn't useful; loading the library is costly in developer and execution time. – GKFX Aug 09 '17 at 18:02
  • This is one solution amongst other, that is viable and direct. And maybe OP want to do non-standard stuff anyway. But thanks to your comment, users may consider not using it, i'll update to _could_ :) – Ulysse BN Aug 09 '17 at 18:04
  • I have access to moment in my project, how do I make my date object a moment object so that I can format it as you suggest? – niko craft Aug 09 '17 at 18:05
  • 1
    `moment(myDate)` should work out of the box – Ulysse BN Aug 09 '17 at 18:06
  • great that worked, how do I get it to use 24 hour instead of 12 hour. I got this val: Wed Aug 09 2017 20:09:51 GMT+0200 (Central Europe Daylight Time) converted to this: 2017-08-09 08:09:51 I would like 2017-08-09 20:09:51 – niko craft Aug 09 '17 at 18:11
  • 1
    Updated accordingly, check [their docs](http://momentjs.com/docs/) also, it's worth it. And consider accepting the answer if it's the one you used :) – Ulysse BN Aug 09 '17 at 18:13
  • Your answer should not rely on a library that is not in the OP or tagged. This is also a duplicate that has been asked many, many times before. – RobG Aug 09 '17 at 23:13