D
D
Dmitry Markov2021-07-06 15:42:07
JavaScript
Dmitry Markov, 2021-07-06 15:42:07

How to get date separator?

How to determine date separator if possible separators are '.', '/', '-' ? The time separator is always ':' so we skip it.
I'm currently using split to split the string and check it 3 times for each of the above characters. I would like to know if this can be done in one step? If so, how?)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
0
0xD34F, 2021-07-06
@En-Tilza

The separator is the first non-numeric character. So . str.match(/\D/)[0]

S
Stalker_RED, 2021-07-06
@Stalker_RED

Often it is enough just to feed the string into new Date() or Date.parse() it takes as input with different delimiters.
If you know the format, you can use moment.js , the format is specified there.
Example:

moment("2010-10-20 4:30",       "YYYY-MM-DD HH:mm");   // parsed as 4:30 local time
moment("2010-10-20 4:30 +0000", "YYYY-MM-DD HH:mm Z"); // parsed as 4:30 UTC

You can do replace(/[.\/]/, '-') and cast everything to hyphens.
If the format is unknown, then it is easy to make a mistake with the date, imagine the sixth of July 2008 recorded in a different order.
'2008/07/06',
  '07-06-2008',
  '06-07-2008',
  '07/06/08',
  '06/07/08',
  '07/06/08',
  '06/07/08',
  '08/06/07',
how do you tell them apart without knowing the format?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question