0

I have an array as follows: enter image description here

I want to add the above array of timestamps inside a dropdown using React dropdown, I have done as follows:

 let new_arr = [];
for (let i = start_ts; i < end_ts; i = i + interval) {
    arr.push(i);
  }
  console.log('arr', arr);
  for (var i = 0; i < arr.length; i += 2) {
    new_arr.push(arr.slice(i, i + 2));
  }
  const defaultoption = new_arr[0];
return (
    <View style={width > 414 ? styles.container_web : styles.container_mob}>
      <View style={styles.inner_container}>
        <TouchableOpacity>
          <Image source={Images.back_icon} style={{height: 15, width: 15}} />
        </TouchableOpacity>
        <Dropdown options={new_arr} value={defaultoption} />
      </View>
    </View>
  );

which resulted it to look as follows: enter image description here enter image description here

However I want the dropdown to look as follows: enter image description here

Any idea or suggestion how should I do it? Any help would be great. Thank you

5
  • can you add more information, what's Dropdown? are you using a 3rd party library of a Dropdown component? Commented Nov 17, 2020 at 15:31
  • Are you looking something like this: Code : ${new Date(1602700200).toDateString()} - ${new Date(1627900200).toDateString()} Result: "Mon Jan 19 1970 - Tue Jan 20 1970" Commented Nov 17, 2020 at 15:38
  • yes @KirankumarAmbati Commented Nov 17, 2020 at 16:17
  • @KirankumarAmbati could you please write a snippet that relates to my code with your logic please? It would be a great help Commented Nov 17, 2020 at 16:22
  • yes, am using React-Dropdown as a package from npm @zb22 Commented Nov 17, 2020 at 16:24

2 Answers 2

1

This should work. Modify the values as per your code:

    let arr = [];
    let new_arr = [];
    let start_ts = 1602700200;
    let end_ts = 1602900200;
    for (let i = start_ts; i < end_ts; i = i + 100000) {
        const date = new Date(i);
        arr.push(`${date.getDate()} ${date.toLocaleString('en-us', { month: 'long' }).slice(0, 3)}`);
      }

      for (var i = 0; i < arr.length; i += 2) {
        new_arr.push(arr.slice(i, i + 2).join(' - '));
      }
      console.log('newarr', new_arr);

result_image

Sign up to request clarification or add additional context in comments.

Comments

0

You can use this to produce the date from timestamp.

const timestamp = 1605680190229;
const date = new Date(timestamp);

console.log(`${date.getDate()} ${date.toLocaleString('en-us', { month: 'long' }).slice(0, 3)}`); // "18 Nov"

From here, I think you can do the rest. I see you already know how to use Dropdown.

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.