0

I am getting a Date as a String in this format from the server yyyyMMdd:hhmmss.

Is there a generic way to format this string to a Date object?

EDIT

 formatDate = (data) => {
    return data.slice(6, 8) + "." + data.slice(4, 6) + "." + data.slice(0, 4) + " " + data.slice(9, 11) + ":" + data.slice(11, 13)
}
Amine
  • 2,241
  • 2
  • 19
  • 41
  • There are many libraries for these kinds of issues like momentjs. Have you tried !? – Ehsan Mar 23 '20 at 15:35
  • I try to stay away from packges for this simle things. I solved this issue with split function but I am not happy with that. – Amine Mar 23 '20 at 15:36
  • I believe that If you are not worried about daylight saving or timezones etc your solution is good enough. – Ehsan Mar 23 '20 at 15:39
  • I will keep my solution for now, maybe someone else has encountered and solved this issue. Thank you anyway – Amine Mar 23 '20 at 15:41
  • Maybe take a look in [here](https://stackoverflow.com/questions/5619202/converting-a-string-to-a-date-in-javascript)? – Robert Feduș Mar 23 '20 at 16:57
  • 1
    The only "generic" way to date string parsing is to use a [valid date time string](https://stackoverflow.com/questions/51715259/what-are-valid-date-time-strings-in-javascript) in the first place. – str Mar 23 '20 at 17:50
  • 1
    "*I solved this issue with split function but I am not happy with that*" Why not? Splitting into parts and passing to the Date constructor is the **only** way. – RobG Mar 23 '20 at 20:09
  • @RobG That would do it. Thanx – Amine Mar 23 '20 at 20:10
  • Cool. If you want to improve your code, post it with whatever you think the issues are and you might receive help with that. ;-) – RobG Mar 23 '20 at 20:12
  • Nice I will post my function for formatting the date – Amine Mar 23 '20 at 20:14
  • 1
    Ok, so what you're doing is just reformatting a string, the fact that it's a date is not important. The way you're doing it is fine, though there are alternatives, e.g. using *match* and *replace*. – RobG Mar 23 '20 at 22:39
  • 1
    _"I try to stay away from packages for this simple things"_ — Dates and Date/Time are _not_ simple things. They are, to the contrary, _very_ complicated to get right. – Stephen P Mar 23 '20 at 23:08
  • @StephenP Yeah i know it's complicated, but in my case I just want to display the date. – Amine Mar 24 '20 at 00:43

1 Answers1

1

The way you're reformatting the string is fine, even though it seems like a lot of code for a small job, slice is pretty fast. Some alternatives (not necessarily "better", just different):

// Reformat yyyyMMdd:hhmmss as dd.mm.yyyy hh:mm:ss
function formatMatch(s) {
  let b = s.match(/\d\d/g) || [];
  return `${b[3]}.${b[2]}.${b[0]}${b[1]} ${b[4]}:${b[5]}:${b[6]}`;
}

function formatReplace(s) {
  return s.replace(/(\d{4})(\d{2})(\d{2}):(\d{2})(\d{2})(\d{2})/, '$3.$2.$1 $4:$5:$6');
}

formatDate = (data) => {
    return data.slice(6, 8) + "." + data.slice(4, 6) + "." + data.slice(0, 4) + " " + data.slice(9, 11) + ":" + data.slice(11, 13)
}

let s = '20200323:123445';
console.log(formatDate(s));
console.log(formatMatch(s));
console.log(formatReplace(s));

If you want to get an actual Date object, then instead of using the bits to create another string, just pass them into the constructor:

// Parse yyyyMMdd:hhmmss to Date object
function parseD(s) {
  let b = s.match(/\d\d/g) || [];
  return new Date(b[0]+b[1], b[2]-1, b[3], b[4], b[5], b[6]);
}

let s = '20200327:134523';
console.log(parseD(s).toString());

The use of || [] means that if there's no match, an empty array is returned so all the b[*] terms return undefined and the result is an invalid date.

The above uses match, but slice or substring can be used the same way.

RobG
  • 142,382
  • 31
  • 172
  • 209