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

Display a byte array in a pictureBox in C#
How to build C# without installing .net framework [closed]
ASP.NET MVC 3 dynamic form generation
Replicating the Win7 network flyout refresh button
How to Read First 512 Bytes of data from a .dat file in C#?
Issue with postbacks on asp.net pages viewed from mobile device
Silverlight - Style ScrollViewer for Listbox
Sharepoint - custom option (button) on list item edit page
Using Console.Read() to determine end of file C#
I don't understand what this error means when working with virtual mouse clicks
Send mail each time with different ip
Timeout expired Exception; Can't find which connection is left open or if there is something else wrong
C# String enum as NHibernate discriminator
Are there any tools that allow me to change all C# built-in types to their .NET Framework types?
“Lock” Excel Workbook for VSTO in C#
C# WebSocket Version 8+ Server Communication

Categories

HOME
actionscript-3
display
android-ndk
liquibase
ebay-api
ms-application-insights
grid
schema
doctrine2
angularjs-ng-repeat
jcodemodel
guidewire
solidity
xmlhttprequest
css-float
vivado-hls
vala
fibonacci
javaagents
functional-dependencies
continuous-fourier
gulp-jshint
large-files
concrete5
schedule
jql
jcr
favorites
node-soap
ilog
openvms
mootools
tunnel
certbot
asp.net-webhooks
iterm2
compiled-query
android-graphview
servicebus
django-cors-headers
ack
confluent
nao-robot
standard-error
extjs3
playframework-1.x
ajv
sp
petapoco
catalyst
vertex-shader
require-handlebars
cexception
abstract
kramdown
transport
traminer
vanilla-forums
syncano
vspackage
stderr
sicstus-prolog
dojox.grid
aiff
register-allocation
sync
vundle
azure-worker-roles
cs193p
django-tests
traceur
elixir-framework
html5-filesystem
db2-connect
ironworker
nokogiri
violation
qgraphicsscene
biztalk-deployment
advanceddatagrid
idtabs
nbehave
n2
qprocess
dojox.gfx
cstring
scalable

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