こと is used to nominalize verbs or clauses, turning them into abstract nouns or concepts. It is used when referring to actions, habits, experiences, or abstract ideas.
~の refers to specific, tangible actions or experiences. It is used more casually in conversation.
~こと refers to abstract actions, concepts, or general ideas. It is more formal or neutral in tone.