My try is 'in temporibus doloris', with ablative form of 'tempus, temporis' and 'dolor, doloris' in the genitive. Is this right or should I maybe be using the dative for pain?
I think your translation is spot-on. The optimal choice depends on context, so my opinion might change if you give a complete sentence in which you want to use it. As a general translation this would be my choice, too. Genitive seems much more appropriate than dative in this phrase.
You can also phrase it in terms of the verb dolere, "to be in pain". As Tom Cotton suggests in a comment, the participle dolens is possible. Sometimes the gerund dolendo might be most appropriate; I might translate "we learn in times of pain" as dolendo discimus. But if there is no sentence context and you want a standalone phrase, your choice is better.