Monday 23 October 2017

Get Local time zone using JavaScript

It is very important to handle date as different countries are in different time zone. Let us check how we can get local time zone. Our output will be EST, PDT, and EDT etc depending on your time zone. You can test this by updating your time zone in your system.

web


var _timezone = null;
function getLocalTimeZone(dateInput)
{
  if ( _timezone ) return _timezone;
  
  var dateObject = dateInput || new Date(),
  
  dateString = dateObject + "",
  
  _timeZoneAbbr = (
    dateString.match(/\(([^\)]+)\)$/) ||
    dateString.match(/([A-Z]+) [\d]{4}$/)
  );
  
  if ( _timeZoneAbbr ) {
    _timeZoneAbbr = _timeZoneAbbr[1].match(/[A-Z]/g).join("");
  }
 
  if (!_timeZoneAbbr && /(GMT\W*\d{4})/.test(dateString)) {
   return RegExp.$1;
 }
 
 
 _timezone = _timeZoneAbbr;
 
 return _timeZoneAbbr;

};

Run the method getLocalTimeZone() and see the result. I have written about date filtering and formatting using angular js 1 and angular 2+ concept as well, if you are using any of this update that code to include this as well for better use case.

Related Info

1. How to show a date is today or yesterday or last week etc using pipe - Angular

2. Disable submit button until all mandatory fields are filled - Angular

3. Angular client side pagination.

4. Angular show more/less pagination

No comments :

Post a Comment