DateTime to short and long String : Date Time Format « Date Time « C# / C Sharp






DateTime to short and long String

    

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Globalization;

namespace ConsultantTime.Web.Utilities.Time
{
    public static class DateTimeExtensions
    {
        public static int WeekNo(this DateTime inDate)
        {
            CultureInfo ciCurr = CultureInfo.CurrentCulture;
            int weekNum = ciCurr.Calendar.GetWeekOfYear(inDate, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
            return weekNum;
        }

        public static string ToReportString(this DateTime inDate)
        {
            return String.Format("{0} {1} {2} {3}", inDate.GetDayNameShort(), inDate.Day, inDate.GetMonthNameShort(), inDate.Year);
        }

        public static string GetDayNameShort(this DateTime inDate)
        {
            switch (inDate.DayOfWeek)
            {
                case DayOfWeek.Monday: return "mon"; break;
                case DayOfWeek.Tuesday: return "tue"; break;
                case DayOfWeek.Wednesday: return "wed"; break;
                case DayOfWeek.Thursday: return "thu"; break;
                case DayOfWeek.Friday: return "fri"; break;
                case DayOfWeek.Saturday: return "sat"; break;
                case DayOfWeek.Sunday: return "sun"; break;
                default: return ""; break;
            }
        }

        public static string GetDayNameLong(this DateTime inDate)
        {
            switch (inDate.DayOfWeek)
            {
                case DayOfWeek.Monday: return "monday"; break;
                case DayOfWeek.Tuesday: return "tuesday"; break;
                case DayOfWeek.Wednesday: return "wednesday"; break;
                case DayOfWeek.Thursday: return "thursday"; break;
                case DayOfWeek.Friday: return "friday"; break;
                case DayOfWeek.Saturday: return "saturday"; break;
                case DayOfWeek.Sunday: return "sunday"; break;
                default: return ""; break;
            }
        }

        public static string GetMonthNameShort(this DateTime inDate)
        {
            switch (inDate.Month)
            {
                case 1: return "jan"; break;
                case 2: return "feb"; break;
                case 3: return "mar"; break;
                case 4: return "apr"; break;
                case 5: return "may"; break;
                case 6: return "jun"; break;
                case 7: return "jul"; break;
                case 8: return "aug"; break;
                case 9: return "sep"; break;
                case 10: return "oct"; break;
                case 11: return "nov"; break;
                case 12: return "dec"; break;
                default: return ""; break;
            }
        }

        public static string GetMonthNameLong(this DateTime inDate)
        {
            switch (inDate.Month)
            {
                case 1: return "january"; break;
                case 2: return "february"; break;
                case 3: return "march"; break;
                case 4: return "april"; break;
                case 5: return "may"; break;
                case 6: return "june"; break;
                case 7: return "july"; break;
                case 8: return "august"; break;
                case 9: return "september"; break;
                case 10: return "october"; break;
                case 11: return "november"; break;
                case 12: return "december"; break;
                default: return ""; break;
            }
        }
    }
}

   
    
    
    
  








Related examples in the same category

1.use the ToString() method to convert a DateTime to a string
2.use the ToLongTimeString() and ToShortTimeString() methods to convert the time parts of a DateTime to long and short time strings
3.use the FromFileTime() method to convert an operating system file timestamp to a DateTime
4.use the ToFileTime() method to convert a DateTime to an operating system file timestamp
5.Format time and date information 1Format time and date information 1
6.Format time and date information 2Format time and date information 2
7.demonstrates culture formattingdemonstrates culture formatting
8.DateTime ToString with CultureInfo
9.The next example formats a DateTime with invariant culture. Invariant culture is always the same, regardless of the computer's settings:
10.Request a specific culture (english language in Great Britain):
11.Compare the results of choosing InvariantInfo with those of choosing CurrentInfo:
12.Format date value as short date format
13.Long date format
14.Sortable date format
15.Universal sortable date format
16.Universal full date/time
17.RFC1123 date format
18.Month date format
19.Year date format
20.General date long time format
21.General date/short time format
22.Full date long time format
23.Full date short time format
24.Long time date format
25.Short time date format
26.Contains helper for converting to and from the date formats
27.DateTime Format: %M
28.DateTime format: MMMM dd, yyyy (dddd)
29.DateTime Format: MM-dd-yy
30.DateTime Format: d, M
31.DateTime Format: d MMMM
32.DateTime Format: dd,MM
33.DateTime Format: ddd d MMM
34.DateTime Format: ddd d MMM (fr-FR)
35.DateTime Format: dddd dd MMMM
36.DateTime Format: dddd dd MMMM (it-IT)
37.DateTime Format: : hh:mm:ss.f
38.DateTime Format: hh:mm:ss.F
39.DateTime Format: hh:mm:ss.ff
40.DateTime Format: hh:mm:ss.FF (2)
41.DateTime Format: "hh:mm:ss.fff
42.DateTime Format: hh:mm:ss.FFF
43.DateTime Format: MM/dd/yyyy g
44.DateTime Format: h:m:s.F t
45.DateTime Format: hh:mm:ss.ff tt
46.DateTime Format: H:mm:ss
47.DateTime Format: HH:mm:ss
48.DateTime Format: %K
49.DateTime Format: hh:mm:ss tt
50.DateTime Format: (M) MMM, MMMM
51.DateTime Format: dd, MM
52.DateTime Format: dddd dd MMMM (2)
53.DateTime Format: yyyyy
54.DateTime Format: %z
55.DateTime Format: zzz, zz
56.DateTime Format: %h
57.DateTime Format: h \\h m \\m
58.Format decimal with different cultures
59.Sql format string to Date
60.dateTime.ToString("dd/MM/yyyy ")
61.dateTime.ToString("dd/MM/yyyy hh:mm")
62.Gets the pretty date.
63.Converts a DateTime object into a unix timestamp number.
64.To Unix time
65.Format Time as ##:##:##
66.Format Time as ##/##/## (2)