When David's son from Bathsheba died David stopped mourning and got up and ate, stating that nothing could be done for his son now:
 Then his servants said to him, “What is this thing that you have done? You fasted and wept for the child while he was alive; but when the child died, you arose and ate food.”  He said, “While the child was still alive, I fasted and wept, for I said, ‘Who knows whether the LORD will be gracious to me, that the child may live?’  But now he is dead. Why should I fast? Can I bring him back again? I shall go to him, but he will not return to me.” (2 Samuel 12:21-23 ESV)
Latter in the same book when David hears that his sons (who was trying to take his throne) is dead he grieved:
 And the king was deeply moved and went up to the chamber over the gate and wept. And as he went, he said, “O my son Absalom, my son, my son Absalom! Would I had died instead of you, O Absalom, my son, my son!” (2 Samuel 18:33 ESV)
David was rebuked for mourning (2 Sam 19ff), but did David not grieve for his baby son as he knew the baby would be in heaven (2 Sam 12:23b) and that Absalom wouldn't be? If this is correct, can this reasoning be applied to all babies who die?
David accepted the death of his child by Bathsheba as punishment for his sin with her and he let that overcome his grief. (The status of the child’s soul is a question for elsewhere.) There was no such consideration in the case of Absalom, and David was overcome by father’s grief. Joab berates him for this, saying that his grief threatens morale.
(Off-topic, but I’ll mention it anyway: The Midrash notes that David called Absalom “my son” eight times between verses 1 & 5; seven to raise him from the seven levels of Gehinnom and the eighth to raise him into Heaven.)Tweet