0

I have 2 input fields; 1 for dates and for for a time. The input for (multiple) dates generates a string like: 2021-05-01,2021-05-02,2021-05-03. If there is set a time in the time input (e.g. 10:00) field, the dates string above should look like this: 2021-05-01T10:00,2021-05-02T10:00,2021-05-03T10:00.

To generate this, i do the following: first check if time input has value. If so, grab all the date values, put a T after them and create a new comma separated string. But i do not know how to put a , between the date-time values

var startdateMulti =  $('.add-startdate').val();  // grab input value
var starttime =  $('.add-starttime').val();  // starttime Multi

var startdateMultiArr = startdateMulti.split(',');
for(var i = 0; i < startdateMultiArr.length; i++) {
    startdateMultiArr[i] = startdateMultiArr[i].replace(/^\s*/, "").replace(/\s*$/, ""); // Trim the excess whitespace.
    if(starttime) { // if there is a starttime filled in
        start += startdateMultiArr[i] + 'T' + starttime;                               
    }
    else {                             
        start += startdateMultiArr[i];                               
    }
}
console.log(start);

If time has filled in, console.log(start) generates something like: 2021-05-01T10:002021-05-02T10:002021-05-03T10:00 and it should be 2021-05-01T10:00,2021-05-02T10:00,2021-05-03T10:00

How can i achieve this?

3 Answers 3

2

You could declare start as array instead string. Then start.push each value to array. Finally start.join(',') with ,

var startdateMulti =  $('.add-startdate').val();  // grab input value
var starttime =  $('.add-starttime').val();  // starttime Multi

//declare as array
var start = []
var startdateMultiArr = startdateMulti.split(',');
for(var i = 0; i < startdateMultiArr.length; i++) {
    startdateMultiArr[i] = startdateMultiArr[i].replace(/^\s*/, "").replace(/\s*$/, ""); // Trim the excess whitespace.
    if(starttime) { // if there is a starttime filled in
        start.push(startdateMultiArr[i] + 'T' + starttime)                             
    }
    else {                             
        start.push(startdateMultiArr[i])                           
    }
}

//array.join with ,
console.log(start.join(','))
Sign up to request clarification or add additional context in comments.

1 Comment

This works great!. Thnx. (dont' forget to put ; behind the lines in the loop)
2

This can be done with a simple map() operation and using trim() for whitespace removal

let dates = $('.add-startdate').val().split(',');
const time = $('.add-starttime').val();

if(time){
  dates = dates.map(s=> `${s.trim()}T${time}`)
}

const res = dates.join(',')

console.log(res)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input class="add-startdate" value="2021-05-01, 2021-05-02, 2021-05-03"/>
<input class="add-starttime" value="10:00"/>

1 Comment

Awesome solution! Only a few lines of code to handle this.
2

As far as I understand you want to combine two strings. You may consider splitting up the string(s), use a reducer to combine and rejoin to the new string. That way there is no need for use of regular expressions1.

Examples:

const datesFromInput = `2021-05-01,2021-05-02,2021-05-03`;
const timeFromInput = "10:00";
const dateTimes = datesFromInput.split(",").reduce( (acc, val, i) => 
  [...acc, `${val}T${timeFromInput}`], []).join(",");
console.log(dateTimes);

// or if there is a (possible) time per date:
const timesFromInput = `10:00,,12:30`.split(",");
console.log( datesFromInput.split(",").reduce( (acc, val, i) => 
  [...acc, `${val}${ timesFromInput[i] && `T${timesFromInput[i]}` }`], [])
    .join(",") );

1 Regular expressions may be a security risk

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.