Skip to content

Instantly share code, notes, and snippets.

@swateek
Last active July 31, 2019 08:44
Show Gist options
  • Save swateek/3a437c31c80f50f3f1552aa56b0f5850 to your computer and use it in GitHub Desktop.
Save swateek/3a437c31c80f50f3f1552aa56b0f5850 to your computer and use it in GitHub Desktop.
Converting a Timezone's Local Time to UTC
#!/usr/bin/python
import moment
# New York's
ny_now_local = moment.utcnow().timezone("America/New_York")
ny_midnight_local = ny_now_local.clone().replace(hours=0, minutes=0, seconds=0, microseconds=0)
ny_prev_midnight_local = ny_midnight_local.clone().subtract(days=1)
ny_prev_lasthour_local = ny_prev_midnight_local.clone().replace(hours=23, minutes=59, seconds=59, microseconds=999999)
ny_prev_midnight_iso = ny_prev_midnight_local.clone().timezone("Etc/UTC").date.isoformat()
ny_prev_lasthour_iso = ny_prev_lasthour_local.clone().timezone("Etc/UTC").date.isoformat()
print(ny_prev_midnight_iso)
print(ny_prev_lasthour_iso)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment