2

I am trying to extract date from a string 20170901000000. new Date(string) returns Invalid Date. The date string looks pretty straightforward to me but apparently javascript doesn't take it.

What formats does javascript new Date() method take? Is there a package taking more formats of date string?

Edit: The formats are from random users. The YYYYMMDDhhmmss is only one example. The package has to be able to determine what format it is by itself and parse it.

zhangjinzhou
  • 2,461
  • 5
  • 21
  • 46
  • is it `yyyymmdd...`? – Eddie May 03 '19 at 16:56
  • It is yyyymmddhhmmss – zhangjinzhou May 03 '19 at 16:57
  • 1
    When in doubt, use the ISO standard. `YYYY-MM-DDTHH:mm:ss.nnn+ZZ` Where Y is the full years, M is the month, padded with a 0, D is the day, padded with a 0, T is the literal letter T, H is the hour out of 24, padded by 0, mm is the minute, padded. ss are seconds, padded, nnn are milliseconds, and +ZZ is the offset. – Madara's Ghost May 03 '19 at 16:57
  • 1
    "String value representing a date. The string should be in a format recognized by the Date.parse() method (IETF-compliant RFC 2822 timestamps and also a version of ISO8601)." https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date – epascarello May 03 '19 at 16:58
  • This YYYYMMDDHHMMSS format is from user. We don't have control on the formats they use – zhangjinzhou May 03 '19 at 16:58
  • 2
    well you need to figure out the format and parse it into a format supported by date – epascarello May 03 '19 at 16:59
  • If you format it like that it needs to be a Unix timestamp, otherwise it needs to be a valid date string specified here https://tools.ietf.org/html/rfc2822#page-14 – Get Off My Lawn May 03 '19 at 17:00
  • Is there any developed package for this? We could get more uncommon formats. – zhangjinzhou May 03 '19 at 17:00
  • 1
    @zhangjinzhou You can try splitting up the string into its pieces, then pass them to `Date`. Or use `moment.js` which can parse a date given a specific format string. – gen_Eric May 03 '19 at 17:00
  • @RocketHazmat Yes I can figure this specific format out easily with regular expression. However, we never know what formats would users use. I am wondering if there is a package already includes a lot of known formats. I believe moment.js only takes iso. – zhangjinzhou May 03 '19 at 17:02
  • @zhangjinzhou You can tell moment.js what format to expect and how to parse it: https://momentjs.com/docs/#/parsing/string-format/ – gen_Eric May 03 '19 at 17:05
  • no one is mentioning date-fns...which is supposed to be much faster than moment – Ctznkane525 May 03 '19 at 17:05
  • @RocketHazmat I don't need any tool if I already known the format. The users are random. They may input whatever date format they want. That's why I am looking for a trained package which knows a lot of formats and can guess what format the user input is. – zhangjinzhou May 03 '19 at 17:10
  • Re: "*Is there a package…*". Requests for off–site resources are off topic. The original question about parsing has been answered many, many times here and there are many resources with information about parsing dates available on the web, such as [*MDN*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse). – RobG May 04 '19 at 12:01

5 Answers5

1

You have to parse your date string:

parseDateString = dateStr =>
  dateStr.replace(
    /^([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})$/,
    '$1-$2-$3 $4:$5:$6'
  );

console.log(new Date(parseDateString('20170901000000')));

Hope this helps,

Miroslav Glamuzina
  • 4,472
  • 2
  • 19
  • 33
1

you can split that string and use this..

new Date (YYYY,MM,DD,HH,MM,SS)

/* from mdn */
new Date(year, monthIndex [, day [, hours [, minutes [, seconds [, milliseconds]]]]])
Excalibur
  • 367
  • 1
  • 7
0

Use a propper library like Moment.js:

const date = moment("20170901000000", "YYYYMMDDhhmmss");
console.log(date);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
arkeros
  • 177
  • 3
  • 2
    This advice is kinda questionable: moment is a *massive* dependency to take on to do something this trivial. – Jared Smith May 03 '19 at 17:04
  • I won't need any tool to parse it if I already known it's "YYYYMMDDhhmmss". The tool has to be able to guess the format. – zhangjinzhou May 03 '19 at 17:06
  • The tool cannot infer automagically the format, since the same string could correspond to many formats. e.g. your string `"20170901"` could be in format `YYYYMMDD` or `YYYDDMM`. – arkeros May 03 '19 at 17:49
  • An answer should not depend on a library that isn't tagged or mentioned in the OP. Parsing the OP string is 2 lines of code, there's no need for a large library like moment.js for such a simple task. – RobG May 04 '19 at 11:56
0

When using a number passed to the date function it must be in the format of a unix timestamp.

new Date(value): A Unix Time Stamp which is an integer value representing the number of milliseconds since January 1, 1970, 00:00:00 UTC, with leap seconds ignored (Unix Epoch; but consider that most Unix timestamp functions count in seconds).

Otherwise you need to generate a valid time string:

new Date(dateString): String value representing a date. The string should be in a format recognized by the Date.parse() method

Since your format is YYYYMMDDHHMMSS and not a unix timestamp we can use the latter dateString approach and use regex to create a valid string date:

let format = '20170901000000'
  .replace(/^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})$/, '$1-$2-$3 $4:$5:$6')

console.log('format:', format)
console.log('date:', new Date(format))
Community
  • 1
  • 1
Get Off My Lawn
  • 34,175
  • 38
  • 176
  • 338
0

Also see the [document for Javascript Date.parse][1]

Examples

new Date('2017-09-01 00:00:00');
new Date('Sept 01, 2017 00:00:00');

For more libraries you can use Moment.js or Day.js

Dai Jie
  • 23
  • 4
  • The day.js works for this sample. However it has it's own issue. For example `dayjs('09012017')` – zhangjinzhou May 03 '19 at 17:27
  • Questions asking for off–site resources are off–topic here, you should not have answered that part of the question. There are a number of date libraries, to answer that part of the question properly would require a review of at least 10 or so of the most popular and best (where "best" is determined by criteria that don't include popularity). – RobG May 04 '19 at 12:05
  • @RobG Got it, I will remove that part, thank you. – Dai Jie May 04 '19 at 14:17