0

Doing this with the date-functions.js library (used e.g. in datetimepicker jQuery plugin):

Date.parseDate('2018-03-10 12:12', 'Y-m-d H:i')

gives:

Sat Mar 10 2018 12:12:00 GMT+0100 (Paris, Madrid)

How to get the result as Unix timestamp or GMT / UTC time instead?

Basj
  • 41,386
  • 99
  • 383
  • 673
  • Please read [Why is extending native objects a bad practice?](https://stackoverflow.com/questions/14034180/why-is-extending-native-objects-a-bad-practice) and avoid using the code from your link. There are many alternative ways to do what you want on StackOverflow, just search for it. – str Mar 14 '18 at 11:12
  • @str I'm using the well-kown https://github.com/xdan/datetimepicker jQuery plugin, that uses [date-functions.js](https://gist.github.com/xaprb/8492729) internally, it works for 99,9% I want, so I don't want to recode this whole jQuery plugin just to use a new date library. (I've spent time searching for other datetimepicker plugins, but this one worked the best). – Basj Mar 14 '18 at 11:17
  • If ECMAScript adds `Date.parseDate` natively, you will have to do that anyway. – str Mar 14 '18 at 11:18
  • Then at this time @str, I'll write *This site is best viewed with Internet Explorer 6 in 1024x768 resolution* and it will solve the new ECMAScript addition problem. – Basj Mar 14 '18 at 11:31
  • @str How would you `ParseDate('2018-03-10 12:12', 'Y-m-d H:i')` and get result either in UTC or UNIX timestamp (assuming the given time is user input in browser local timezone) without this library but standard JS? – Basj Mar 14 '18 at 11:54
  • 1
    If any of the answers resolved your problems please accept it. Thanks – phuzi Mar 15 '18 at 09:56

3 Answers3

1

A string like '2018-03-10 12:12' will usually be parsed as local as there is no timezone offset. It's also not ISO 8601 compliant so using the built-in parser will yield different results in different browsers.

While you can use a library, to parse it as UTC and get the time value is just 2 lines of code:

function toUTCTimeValue(s) {
  var b = s.split(/\D/);
  return Date.UTC(b[0],b[1]-1,b[2],b[3],b[4]);
}

// As time value
console.log(toUTCTimeValue('2018-03-10 12:12'));

// Convert to Date object and print as timestamp
console.log(new Date(toUTCTimeValue('2018-03-10 12:12')).toISOString());
RobG
  • 142,382
  • 31
  • 172
  • 209
0

Use MomentJS instead. You can specify exactly what format the string you're parsing is in. MomentJS can then provide you with the underlying Date object, unix timestamp as well as convert to UTC.

var d = moment('2018-03-10 12:12', 'YYYY-MM-DD HH:mm');
console.log(d.toDate());
console.log(d.unix());
console.log(d.utc().toDate());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.21.0/moment.min.js"></script>

You could of course also parse the date as UTC too instead of treating it as a local time.

moment.utc('2018-03-10 12:12', 'YYYY-MM-DD HH:mm');

NOTE Bit difficult for me to test UTC as I'm in the UK and GMT and UTC are virtually the same.

phuzi
  • 12,078
  • 3
  • 26
  • 50
  • You UK developers are lucky to not have to deal with timezones problems :) – Basj Mar 14 '18 at 12:47
  • @Basj lol, it's more of a problem for us because it's something that often gets overlooked, then we wonder why things don't work. We still get to worry about daylight saving(s) too. – phuzi Mar 14 '18 at 12:58
  • That's right :) ps: i don't remember: do you have daylight saving time change or don't you? – Basj Mar 14 '18 at 13:04
  • 1
    @Basj yep. We transition to BST (British Summer Time) at end of March and then back at end of October. It's usually daylight saving(s) that is the biggest problem as it changes at different times for different timezones (if at all.) – phuzi Mar 14 '18 at 13:08
  • That will parse it as local, so different result in each timezone with a different offset. – RobG Mar 14 '18 at 13:11
  • @RobG Yep, that'll be why OP wants UTC/Unix timestamp. Good practice to always deal with UTC. – phuzi Mar 14 '18 at 13:12
  • @phuzi—as I understand it, the OP wants it parsed as UTC, not local. But on re–reading I'm not sure what is required, it's really not clear, but your answer should state what it's doing, not just post some code. – RobG Mar 14 '18 at 13:31
  • @RobG Hmm, I suppose that's a possibility, but given that date time is coming from a date picker and a user would naturally pick a local date time, rather than do the conversion in their head before picking. Bit of an ask from a UX point of view and not everyone knows or cares what the current offset to UTC is or even apply it correctly. – phuzi Mar 14 '18 at 13:36
0

var date = new Date('2018-03-10 12:12'.replace(' ', 'T'));

// Unix
console.log(Math.floor(date.getTime() / 1000));
    
// UTC
console.log(date.toUTCString());

As always, please have a look at the documentation at MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

str
  • 42,689
  • 17
  • 109
  • 127