1

I tried passing the values as Json (key-pair values) but I don't get to to see the values in response.

Abhirup Mukherjee
  • 499
  • 1
  • 8
  • 21

3 Answers3

2

To store current date and time in AsyncStorage you can use an async function,

StoreDate = async () => {
  await AsyncStorage.setItem('date', new Date());
};

To retrieve stored date,

retrieveDate = async () => {
    const value = await AsyncStorage.getItem('date');
};
Banid Azin
  • 180
  • 2
  • 17
  • Thanks a lot Azin!! That did help me. I am getting the output as: value 2019-05-27T05:28:30.708Z. Can you help me out how can I convert/modify the date format? – Abhirup Mukherjee May 27 '19 at 05:30
  • Welcome @AbhirupMukherjee, please accept the answer – Banid Azin May 27 '19 at 05:31
  • I am getting the output as: value 2019-05-27T05:28:30.708Z. Can you help me out how can I convert/modify the date format? @BanidAzin – Abhirup Mukherjee May 27 '19 at 06:08
  • you can convert it using the library moment. Refer official documentation (https://momentjs.com/) – Banid Azin May 27 '19 at 06:35
  • i'm trying to store the date values while the state of app changes. And when the app is in background I want to store the date/time value in asyncstorage and return it. But the promise values all I get is undefined.. My RN version is 56.0. Can you help me out? Thanks in advance. – Abhirup Mukherjee May 28 '19 at 04:09
  • can you show me the code. Doesn't know much about background working in RN, but I will try to look into that – Banid Azin May 28 '19 at 07:15
  • Hi @BanidAzin if I am passing new Date().getTime() instead of new Date() in StoreDate method. I get an error saying: JSON Value ' ' of type NSNumber cannot be converted to NSString. – Abhirup Mukherjee May 29 '19 at 06:40
  • Any approach to solve this using parse or any other way? Please let me know. Thanks in advance! – Abhirup Mukherjee May 29 '19 at 06:41
  • I couldn't reproduce the problem. please show me the code or make a snack. so I can check on that. sry for the late reply, I will reach out to you if I find anything. – Banid Azin May 29 '19 at 14:39
  • Hi @BanidAzin I figured out the problem and it works fine. Thanks for your effort to help me out. – Abhirup Mukherjee May 30 '19 at 04:32
  • Hi, That's great. @AbhirupMukherjee – Banid Azin May 30 '19 at 05:11
1

If you want to get value directly in a variable then you should use Async/await

then in your code you can get data like this

let data = await AsyncStorage.getItem('data');

to convert data to json

jsonData = JSON.parse(data);
Vinil Prabhu
  • 1,279
  • 1
  • 10
  • 22
0

Try to resolve the promise:

getItem({ key: value}).then(data => console.log(data);
  • I tried resolving the promise by using filter. then((filter)=>{ console.log("filter",filter); }); But I can't see the values being stored/returned back in the response. – Abhirup Mukherjee May 27 '19 at 01:14