c#


how to get TimeZoneInfo short name


Is there any method to get the 3 char code from
System.TimeZoneInfo.Local
?
e.g. EDT instead of Eastern Daylight time etc.
Unfortunately, there is no easy built-in way of doing this that I know of. However, you could put something together yourself. Here's an example:
public static class TimeZoneInfoExtensions {
public static string Abbreviation(this TimeZoneInfo Source) {
var Map = new Dictionary<string, string>()
{
{"eastern standard time","est"},
{"mountain standard time","mst"},
{"central standard time","cst"},
{"pacific standard time","pst"}
//etc...
};
return Map[Source.Id.ToLower()].ToUpper();
}
}
Use as follows:
string CurrentTimeZoneAbbreviation = System.TimeZoneInfo.Local.Abbreviation();
If you need more conversions you could just plug them into the Map dictionary.
TimeZoneInfo.Id will be a string matching a given key in [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones]. If you can find a matching database online, containing the same Ids as well as the abbreviations, it would be possible to quickly extract and import the pairs (with regular expressions, for example) and drop those into the Map dictionary.
You can write something like:
var abbr = System.TimeZoneInfo.Local.TimeZoneAbbr();
And the helper for it:
public static class ConvertionHelper
{
public static String TimeZoneAbbr(this TimeZoneInfo zone)
{
var zoneName = zone.Id;/* zone.IsDaylightSavingTime(DateTime.UtcNow)
? zone.DaylightName
: zone.StandardName;*/
var zoneAbbr = zoneName.CapitalLetters();
return zoneAbbr;
}
public static String CapitalLetters(this String str)
{
return str.Transform(c => Char.IsUpper(c)
? c.ToString(CultureInfo.InvariantCulture)
: null);
}
private static String Transform(this String src, Func<Char, String> transformation)
{
if (String.IsNullOrWhiteSpace(src))
{
return src;
}
var result = src.Select(transformation)
.Where(res => res != null)
.ToList();
return String.Join("", result);
}
}

Related Links

Initialization with null-coalescing operator?
Which c# class should be used when parsing XML from a remote URI?
How to remove text from a specific line in file without creating unwanted space using c#?
C# adding +5 to a datatable if criteria is met
Layout page not found executing a sample ASP.NET MVC project
How can I perform a binary search on an Excel range to find the last non-null cell?
Why do I need to use Console.ReadLine() to view output text? [duplicate]
Unhandled exception not caught by handler
Multiple documentation created for the same API
Set the SendTimeout on a WCF client callback binding?
CNTKLibrary-2.0 “System.AccessViolationException: Attempted to read or write protected memory. ”
How to access client side scripts from Source View smart tag designer by using GetClientScriptsInDocument
After some idle time images not load from database
C# string contains daterange which should splitted into 2 dates
Div “modal-content” control does not appear until mouse pointer is hover on it while running on remote machine
Defining Expected filenames from Directory.GetFiles

Categories

HOME
autohotkey
swift3
alasql
domain-driven-design
packet
arcgis
ebay
angularjs-directive
osx-yosemite
segmentation-fault
openbsd
google-search-appliance
mamp
suitecrm
opendaylight
exchange-transport-agents
tag-cloud
pylint
jquery-easyui
passbook
ampscript
sasl
symlink
ballerinalang
multiple-inheritance
hierarchical-data
spock
restangular
r-factor
ocl
mobile-development
openoffice-impress
jcr
tunnel
geocomplete
pyenv
blueimp
tapply
compiler-warnings
datagridviewcombobox
vpython
peerjs
grails-2.0
hierarchical-clustering
react.rb
hdpi
denodo
beanshell
senti-wordnet
grib
overhead
boost-range
networkstream
nsoperationqueue
system-integration
3d-rendering
wordpress-plugin
dynamics-nav
updating
windows-phone-8-emulator
om
callfire
eggplant
android-networking
screen-lock
genero
service-broker
data-quality-services
dockerpy
numerical-recipes
nspopupbutton
ant-contrib
nodeunit
connections
.net-reflector
db2-connect
mdp
mysql-error-1146
raddatepicker
infomaker
symbian3
correctness
expresso-store
netbiscuits
socketasynceventargs
animationdrawable
win-prolog
capitalization
rjs
log-shipping
xhtmlrenderer
designated-initializer
excellibrary
rollover
system-analysis
web-search
user-preferences
ffdshow
content-delivery-network
ironpython-studio
paperless

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App