Get a Cosmog

After becoming Champion, you can use your legendary pokémon to get a Cosmog.

Go to the Other World

This can be done after you have defeated the Elite Four and become the Alola Champion.

If you have Solgaleo, bring it to the Altar of the Sunne at night, and you can go through the rift there to daytime. If you have Lunala, bring it to the Altar of the Moone during the day, and you can go through the rift to night time. You can do this no matter which version of the game you are playing, as long as you have the Legendary with you.

This will allow you to catch pokémon that only appear at certain times of day, but you will NOT be able to get the version exclusives from the other game.

If you are curious to know what time it is in this parallel world, go to a Pokémon Center and talk to the guy behind the counter of the Café.

Go to the Lake to get Cosmog

After you have gone through the rift, take your legendary with you to the Lake of the Sunne/Moone and go to the center. Cosmog will appear. Check on Cosmog to take it with you.

Cosmog evolves into Cosmoem at level 43. In Pokémon Sun, Cosmoem will evolve into Solgaleo at level 53. In Pokémon Moon, Cosmoem will evolve into Lunala at level 53.