sábado, 17 de abril de 2010

Dicas Visual Studio (C#) - String Format para DateTime

Salve, salve

Abaixo, alguns exemplos de como utilizar String.Format para formar datas.

// Criando uma variavel do tipo date time 2008-03-09 16:05:07.123

DateTime dt = new DateTime(2008, 3, 9, 16, 5, 7, 123);

String.Format("{0:y yy yyy yyyy}", dt); // "8 08 008 2008" ano
String.Format("{0:M MM MMM MMMM}", dt); // "3 03 Mar March" mês
String.Format("{0:d dd ddd dddd}", dt); // "9 09 Sun Sunday" dia
String.Format("{0:h hh H HH}", dt); // "4 04 16 16" horas 12/24
String.Format("{0:m mm}", dt); // "5 05" minutos
String.Format("{0:s ss}", dt); // "7 07" segundos
String.Format("{0:f ff fff ffff}", dt); // "1 12 123 1230" seg.fracionados
String.Format("{0:F FF FFF FFFF}", dt); // "1 12 123 123" sem zeros
String.Format("{0:t tt}", dt); // "P PM" até 12 A.M ou após P.M.
String.Format("{0:z zz zzz}", dt); // "-6 -06 -06:00" time zone


Voce pode usar também um separador de data / (barra) e um time separador : (pontos).
Estes caracteres irão ser reescritos para o caracter definido em um current DateTimeForma­tInfo.DateSepa­rator and DateTimeForma­tInfo.TimeSepa­rator.

// date separator in german culture is "." (so "/" changes to ".")

String.Format("{0:d/M/yyyy HH:mm:ss}", dt); // "9/3/2008 16:05:07" - english (en-US)
String.Format("{0:d/M/yyyy HH:mm:ss}", dt); // "9.3.2008 16:05:07" - german (de-DE)


// Aqui estão alguns exemplos de customizacao de data e hora sendo formatadas

// mes/dia numeros sem/com zeros

String.Format("{0:M/d/yyyy}", dt); // "3/9/2008"
String.Format("{0:MM/dd/yyyy}", dt); // "03/09/2008"

// dia/nome dos meses
String.Format("{0:ddd, MMM d, yyyy}", dt); // "Sun, Mar 9, 2008"
String.Format("{0:dddd, MMMM d, yyyy}", dt); // "Sunday, March 9, 2008"

// dois/quatro digitos para ano
String.Format("{0:MM/dd/yy}", dt); // "03/09/08"
String.Format("{0:MM/dd/yyyy}", dt); // "03/09/2008"


// Padrão de formatação para DateTime

Especificador DateTimeFormatInfo Valor padrão (for en-US culture)
t ShortTimePattern h:mm tt
d ShortDatePattern M/d/yyyy
T LongTimePattern h:mm:ss tt
D LongDatePattern dddd, MMMM dd, yyyy
f (combination of D and t) dddd, MMMM dd, yyyy h:mm tt
F FullDateTimePattern dddd, MMMM dd, yyyy h:mm:ss tt
g (combination of d and t) M/d/yyyy h:mm tt
G (combination of d and T) M/d/yyyy h:mm:ss tt
m, M MonthDayPattern MMMM dd
y, Y YearMonthPattern MMMM, yyyy
r, R RFC1123Pattern ddd, dd MMM yyyy HH':'mm':'ss 'GMT' (*)
s SortableDateTi­mePattern yyyy'-'MM'-'dd'T'HH':'mm':'ss (*)

(*) = culture independent


// Abaixo, examplos usando o formato padrão utilizando os especificadores

String.Format("{0:t}", dt); // "4:05 PM" ShortTime
String.Format("{0:d}", dt); // "3/9/2008" ShortDate
String.Format("{0:T}", dt); // "4:05:07 PM" LongTime
String.Format("{0:D}", dt); // "Sunday, March 09, 2008" LongDate
String.Format("{0:f}", dt); // "Sunday, March 09, 2008 4:05 PM" LongDate+ShortTime
String.Format("{0:F}", dt); // "Sunday, March 09, 2008 4:05:07 PM" FullDateTime
String.Format("{0:g}", dt); // "3/9/2008 4:05 PM" ShortDate+ShortTime
String.Format("{0:G}", dt); // "3/9/2008 4:05:07 PM" ShortDate+LongTime
String.Format("{0:m}", dt); // "March 09" MonthDay
String.Format("{0:y}", dt); // "March, 2008" YearMonth
String.Format("{0:r}", dt); // "Sun, 09 Mar 2008 16:05:07 GMT" RFC1123
String.Format("{0:s}", dt); // "2008-03-09T16:05:07" SortableDateTime


Agora, basta praticat para ver os resultados.

Take care

10 comentários:

Ronaldo Almeida disse...

Faleu Marquim esta dica vai me ajudar muito na correção do 1º trabalho e na produção do 2º Trabalho de desenvolvimento .Net.

Agradeço muito pelas dicas.

Henrique P. Boueri disse...

Very interesting and usefull tips, Marking.
Thank you very much.
See you soon.
BB!

Unknown disse...

Valeu a dica

Anônimo disse...

These are rendered tiles, neopets cheats tool and then people who
want to hang out, Bruce has time to stay this afternoon.
Then at the end of a row ñ at the end of the screen.


Also visit my page - Neopets Hack

Anônimo disse...

beats lady gaga
Howdy just wanted to give you a quick heads up.
The text in your content seem to be running off the
screen in Firefox. I'm not sure if this is a formatting issue or something to do with internet browser compatibility but I thought
I'd post to let you know. The layout look great though!
Hope you get the issue solved soon. Kudos

Anônimo disse...

Howdy! This is kind of off topic but I need some
help from an established blog. Is it difficult to set up
your own blog? I'm not very techincal but I can figure things out pretty fast.
I'm thinking about creating my own but I'm not sure where to begin. Do you
have any ideas or suggestions? Appreciate it

Also visit my webpage ... Cheap league of legends skin codes by FreeLeagueOfLegendsCodes.com

Anônimo disse...

celine bags cheap
Hi, I read your blog regularly. Your story-telling style is
awesome, keep doing what you're doing!

Anônimo disse...

The dilemma is how to build without disturbing any antiquities that may
lie buried and undiscovered below its spectacular roads and streets.

After discovering he's to be slaughtered for Christmas dinner, Charlotte concocts a plan to save Wilbur.
Careers in ships as ship engineers or assistant engineers rank amongst the best paying blue collar jobs.


Also visit my weblog: subway surfers hack android

Anônimo disse...

Excellent, what a website it is! This weblog presents helpful information to us, keep it up.


my blog; Find The Most Affordable Golf-Get-Aways In Myrtle Beach This Season

Anônimo disse...

Enjoy reading it. Based off modern research, Garcinia is used globally as a weight loss supplement for
the people experiencing some sort of excess weight. It comes by a all natural base and
does not possess detrimental chemicals, preservatives or carcinogens that could cause harm
to the body. By filled with hydroxycitric acid, the following could
be a excess weight blocker, which is required to weight-loss.
Hemorrhoids Treatment Cure success stories helps to
people to suppress their appetites and minimize sugars or desserts thinking about
eating. Forget about running towards the kitchen each A quarter-hour to get a
treat. Actually, the authentic Garcinia Cambogiawill equalize the
amount of a individual stress growth hormones. This helps to stop prospective psychological over eating, which can be one of the root factors for getting fatter.


Feel free to visit my page; hemorrhoids video surgery