Typically doctors do the first ultrasound around 18 to 21 weeks, but you can have it as early as 14 weeks in some cases. If the baby is in a convenient position, you may get a glimpse at their external sexual organs then. However, this isn't always possible or accurate to gauge gender. Here (https://www.ncbi.nlm.nih.gov/pmc/articles/PMC5824932/) is the National Library of Medicine's explanation.