c# - DateTime Custom Formatting for Seasons -


i've come across problem customizing datetimepicker value in application. i've read various formatting strings can use customize way date/time interpreted. issue want of text ignored in custom format string can add in season string beginning of datetimepicker.

for example, let's take today's date august 7th, 2013 @ 5:30pm (in us). if use custom format string "mmm.d -h:mm tt" date shown aug. 7th - 5:30pm. so, that's perfect. only, want add season beginning of string. so, in case, "summer: aug. 7th - 5:30pm".

the issue i'm having if insert word "summer" @ beginning of custom format string, interprets double mm's getminute value of datetime. i'd season remain literal, rest of format string interpreted (if makes sense).

here code i'm using:

public form1() {     initializecomponent();     datetimepicker1.format = datetimepickerformat.custom;     season = getseason(datetimepicker1.value);     datetimepicker1.customformat = convertseason(season) + " : " + dt_format; }  public int season = 1;  //set default summer public string dt_format = "mmm.d  -h:mm tt";  private int getseason(datetime date) {     float value = (float)date.month + date.day / 100;   // <month>.<day(2 digit)>     if (value < 3.21 || value >= 12.22) return 3;   // winter     if (value < 6.21) return 0; // spring     if (value < 9.23) return 1; // summer     return 2;   // autumn } private string convertseason(int value) {     string season = "spring";     if (value == 1) season = "summer";     else if (value == 2) season = "autumn";     else if (value == 3) season = "winter";     return season; }  private void datetimepicker1_valuechanged(object sender, eventargs e) {     season = getseason(datetimepicker1.value);     datetimepicker1.customformat = convertseason(season) + " : " + dt_format; } 

you need surround in literal string delimiter (for datetime format strings): '.

so method this:

private string convertseason(int value) {     string season = "'spring'";     if (value == 1) season = "'summer'";     else if (value == 2) season = "'autumn'";     else if (value == 3) season = "'winter'";     return season; } 

however, methods improved bit. took liberty of doing that:

private int getseason(datetime date) {     //using decimal avoid inaccuracy issues     decimal value = date.month + date.day / 100m;   // <month>.<day(2 digit)>     if (value < 3.21 || value >= 12.22) return 3;   // winter     if (value < 6.21) return 0; // spring     if (value < 9.23) return 1; // summer     return 2;   // autumn } private string convertseason(int value) {     switch (value)     {         case 0:             return "'spring'";         case 1:             return "'summer'";         case 2:             return "'autumn'";         case 3:             return "'winter'";     }     return ""; } 

Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -