0

I have string date for example '20200321082245'. I am trying to convert it by pure Javaccript code to a date object by spliting it and creating an date object for example:

'20200321082245' --> 2020-03-21 08:22:45

'202003210.2245' --> This case with a dot must be excluded. I must return a null date object

How can I convert it correctly ? Since the syncDate could contain a dot also for example a double number also not integer.

My Code:

        function convertDate(syncDate){
            var date = null;
            if(syncDate){
            try {
                if (!isNaN(syncDate)) {
                        var d = syncDate.toString().match(/.{1,2}/g)
                        var len = d.length;
                        var dateString = '';
                        switch(len){
                            case 2:
                                dateString = d[0]+d[1];
                            case 3:
                                dateString = d[0]+d[1] +'-'+d[2]
                            case 4:
                                dateString = d[0]+d[1] +'-'+d[2]+'-'+d[3]
                            case 5:
                                dateString = d[0]+d[1] +'-'+d[2]+'-'+ d[3]+' '+d[4]
                            case 6:
                                dateString = d[0]+d[1] +'-'+d[2]+'-'+d[3]+' '+d[4]+':'+d[5];
                            case 7:
                                dateString = d[0]+d[1] +'-'+d[2]+'-'+d[3]+' '+d[4]+':'+d[5]+':'+d[6];
                        }
                      if(dateString)
                         date = new Date(dateString);
                    }
                } catch (e) {}
            }

            return date;
         }
3
  • You're already using a regular expression. Use one that matches all of the date parts, convert the parts to actual numbers and construct the date with them. Commented Apr 22, 2020 at 14:40
  • @Andreas: something like /^\d+$/.test(syncDate). Sorry but I am not well familiar with regex. Commented Apr 22, 2020 at 14:51
  • Constructing the date like I am doing in the switch parts? Commented Apr 22, 2020 at 14:55

2 Answers 2

2
var value = '20200321082245';
var dateRx = /(^\d\d\d\d)(\d\d)(\d\d)(\d\d)?(\d\d)?(\d\d)?/;
if (dateRx.test(value)) {
    return new Date(value.replace(dateRx, '$1-$2-$3 $4:$5:$6'));
}
return null;
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for your answer, What about this one '20200321082'.replace(/(^\d\d\d\d)(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)/, '$1-$2-$3 $4:$5:$6') -->I am getting as result the following '20200321082'
I am not getting a result for var value = '2020032108'; since new Date('2020-03-21 08:') is an date object
Therefore I am building the string in the switch part
You did not provide such an example in your question. Write your question more clearly and you'll get a right answer
Anyway, you can achieve what you want by making last three groups optional. Answer edited.
0

You can check if the string contains a . by using the includes property.

if(string.includes('.')){
    return null;
}

About converting it into a date object, there are several ways to achieve this. See here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.