Skip to content

Instantly share code, notes, and snippets.

@ocristian
Last active April 7, 2016 06:08
Show Gist options
  • Save ocristian/085768fecae7a618b41db2e558153445 to your computer and use it in GitHub Desktop.
Save ocristian/085768fecae7a618b41db2e558153445 to your computer and use it in GitHub Desktop.
to find the current local date and time based on user system
// Example: 4/7/2016 3:00 AM BRT
function currentLocalDateTime(){
var date = new Date();
var curr_year = date.getFullYear();
var curr_month = ("00" + (date.getMonth() + 1)).slice(-2);
var curr_dayOfMonth = ("00" + date.getDate()).slice(-2);
var curr_hour = date.getHours();
var curr_minute = ("00" + date.getMinutes()).slice(2);
var am_pm = curr_hour > 12 ? 'PM' : 'AM';
var dateString = date.toString().split(" ");
var time_zone_abbr = dateString.pop().replace(/[^a-zA-Z]/g, '');
var current_local_time = curr_month.concat('/').concat(curr_dayOfMonth).concat('/').concat(curr_year).concat(' ').concat(curr_hour).concat(':').concat(curr_minute).concat(' ').concat(am_pm).concat(' ').concat(time_zone_abbr);
console.log('Current local time: ' + current_local_time);
return current_local_time;
}
@ocristian
Copy link
Author

Very nice with MomentJS :)

First, import the dependencies:

    <script src="js/moment.js"></script>
    <script src="js/moment-timezone-with-data.js"></script>

and them:

    moment.tz(new Date(), moment.tz.guess()).format('l LT z')`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment