zoukankan      html  css  js  c++  java
  • Metro style App Datetime formating.

    Key Code:

    1. Long and short Format

            private void Display_Click(object sender, RoutedEventArgs e)
            {
                // We keep results in this variable
                StringBuilder results = new StringBuilder();
    
                // Create basic date/time formatters.
                DateTimeFormatter[] basicFormatters = new[]
                {
                    // Default date formatters
                    new DateTimeFormatter("shortdate"),
                    new DateTimeFormatter("longdate"),
    
                    // Default time formatters
                    new DateTimeFormatter("shorttime"),
                    new DateTimeFormatter("longtime"),
                 };
    
                // Create date/time to format and display.
                DateTime dateTime = DateTime.Now;
    
                // Try to format and display date/time if calendar supports it.
                foreach (DateTimeFormatter formatter in basicFormatters)
                {
                    // Format and display date/time.
                    results.AppendLine(formatter.Template + ": " + formatter.Format(dateTime));
                }
            }
    

    2. Use different parameters.

            private void Display_Click2(object sender, RoutedEventArgs e)
            {
                // This scenario uses the Windows.Globalization.DateTimeFormatting.DateTimeFormatter class
                // to format a date/time by specifying a template via parameters.
    
                // We keep results in this variable
                StringBuilder results = new StringBuilder();
    
                // Create formatters with individual format specifiers for date/time elements.
                DateTimeFormatter[] templateFormatters = new[]
                {
                    // Example formatters for dates.
                    new DateTimeFormatter(
                        YearFormat.Full, 
                        MonthFormat.Abbreviated, 
                        DayFormat.Default, 
                        DayOfWeekFormat.Abbreviated),
                    new DateTimeFormatter(
                        YearFormat.Abbreviated, 
                        MonthFormat.Abbreviated, 
                        DayFormat.Default, 
                        DayOfWeekFormat.None),
                    new DateTimeFormatter(
                        YearFormat.Full, 
                        MonthFormat.Full, 
                        DayFormat.None, 
                        DayOfWeekFormat.None),
                    new DateTimeFormatter(
                        YearFormat.None, 
                        MonthFormat.Full, 
                        DayFormat.Default, 
                        DayOfWeekFormat.None),
    
                    // Example formatters for times.
                    new DateTimeFormatter(
                        HourFormat.Default, 
                        MinuteFormat.Default, 
                        SecondFormat.Default),
                    new DateTimeFormatter(
                        HourFormat.Default, 
                        MinuteFormat.Default, 
                        SecondFormat.None),
                    new DateTimeFormatter(
                        HourFormat.Default, 
                        MinuteFormat.None, 
                        SecondFormat.None),
                 };
    
                // Create date/time to format and display.
                DateTime dateTime = DateTime.Now;
    
                // Try to format and display date/time if calendar supports it.
                foreach (DateTimeFormatter formatter in templateFormatters)
                {
                    // Format and display date/time.
                    results.AppendLine(formatter.Template + ": " + formatter.Format(dateTime));
                }
            }
    

    3. use String Template

            private void Display_Click3(object sender, RoutedEventArgs e)
            {
                // We keep results in this variable
                StringBuilder results = new StringBuilder();
    
                // Create template-based date/time formatters.
                DateTimeFormatter[] templateFormatters = new[]
                {
                    // Formatters for dates.
                    new DateTimeFormatter("day month"),
                    new DateTimeFormatter("month year"),
                    new DateTimeFormatter("day month year"),
                    new DateTimeFormatter("dayofweek day month year"),
                    new DateTimeFormatter("dayofweek.abbreviated"),
                    new DateTimeFormatter("month.abbreviated"),
                    new DateTimeFormatter("year.abbreviated"),
    
                    // Formatters for time.
                    new DateTimeFormatter("hour"),
                    new DateTimeFormatter("hour minute"),
                    new DateTimeFormatter("hour minute second"),
                 };
    
    
                // Create date/time to format and display.
                DateTime dateTime = DateTime.Now;
    
                // Try to format and display date/time if calendar supports it.
                foreach (DateTimeFormatter formatter in templateFormatters)
                {
                    // Format and display date/time.
                    results.AppendLine(formatter.Template + ": " + formatter.Format(dateTime));
                }
            }
    

     C++ code

    1 Long and short format

    void App2::MainPage::Button_Click_1(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
    {
    	Array<DateTimeFormatter^> ^dateFormatters = {
    		ref new DateTimeFormatter("shortdate"),
    		ref new DateTimeFormatter("longdate"),
    		ref new DateTimeFormatter("longtime")
    	};
    
    	Windows::Globalization::Calendar^ cal = ref new Windows::Globalization::Calendar();
    	Windows::Foundation::DateTime dateToFormat = cal->GetDateTime();
    
    	String^ results = "";
    
    	for (unsigned int i = 0; i < dateFormatters->Length; i++)
    	{
    		results = results + dateFormatters[i]->Template + ": " + dateFormatters[i]->Format(dateToFormat) +"\n";
    	}
    }
    

    2. Use different parameter

    void App2::MainPage::Button_Click_2(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
    {
        Array<DateTimeFormatter^> ^dateFormatters = {
    		ref new DateTimeFormatter(
    			YearFormat::Full, 
    			MonthFormat::Abbreviated, 
    			DayFormat::Default, 
    			DayOfWeekFormat::Abbreviated),
    
    		ref new DateTimeFormatter(
    			YearFormat::Abbreviated, 
    			MonthFormat::Abbreviated, 
    			DayFormat::Default, 
    			DayOfWeekFormat::None),
    
    		ref new DateTimeFormatter(
    			YearFormat::Full, 
    			MonthFormat::Full, 
    			DayFormat::None, 
    			DayOfWeekFormat::None),
    
            ref new DateTimeFormatter(
    			YearFormat::None, 
    			MonthFormat::Full, 
    			DayFormat::Default, 
    			DayOfWeekFormat::None),
    
    		ref new DateTimeFormatter(
    			HourFormat::Default, 
    			MinuteFormat::Default, 
    			SecondFormat::Default),
    
    		ref new DateTimeFormatter(
    			HourFormat::Default, 
    			MinuteFormat::Default, 
    			SecondFormat::None),
    
    		ref new DateTimeFormatter(
    			HourFormat::Default, 
    			MinuteFormat::None, 
    			SecondFormat::None)
        };
    
    	// Obtain the date that will be formatted.
    	Windows::Globalization::Calendar^ cal = ref new Windows::Globalization::Calendar();
        Windows::Foundation::DateTime dateToFormat = cal->GetDateTime();
        
        // Keep the results here
        String^ results = "";
    
    	// Generate the results.
        for (unsigned int i = 0; i < dateFormatters->Length; i++)
        {
            // Perform the actual formatting. 
            results = results + dateFormatters[i]->Template + ": " + dateFormatters[i]->Format(dateToFormat) + "\n"; 
        }
    }
    

    3.Use String Template

    void App2::MainPage::Button_Click_3(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
    {
    	Array<DateTimeFormatter^> ^dateFormatters = {
    		ref new DateTimeFormatter("month day"),
    		ref new DateTimeFormatter("month year"),
    		ref new DateTimeFormatter("month day year"),
    		ref new DateTimeFormatter("month day dayofweek year"),
    		ref new DateTimeFormatter("dayofweek.abbreviated"),
    		ref new DateTimeFormatter("month.abbreviated"),
    		ref new DateTimeFormatter("year.abbreviated"),
    		ref new DateTimeFormatter("hour minute"),
    		ref new DateTimeFormatter("hour minute second"),
    		ref new DateTimeFormatter("hour")
    	};
    
    	// Obtain the date that will be formatted.
    	Windows::Globalization::Calendar^ cal = ref new Windows::Globalization::Calendar();
    	Windows::Foundation::DateTime dateToFormat = cal->GetDateTime();
    
    	// Keep the results here
    	String^ results = "";
    
    	// Generate the results.
    	for (unsigned int i = 0; i < dateFormatters->Length; i++)
    	{
    		results = results + dateFormatters[i]->Template + ": " + dateFormatters[i]->Format(dateToFormat) + "\n";
    	}
    
    }
    

    From Metro style app samples.

     

    作者:Work Hard Work Smart
    出处:http://www.cnblogs.com/linlf03/
    欢迎任何形式的转载,未经作者同意,请保留此段声明!

  • 相关阅读:
    Java 利用SWFUpload多文件上传 session 为空失效,不能验证的问题 swfUpload多文件上传
    对ExtJS4应用 性能优化的几点建议
    Extjs4中用combox做下拉带图片的下拉框
    当你的才华还撑不起你的野心时,就应该静下心来学习(转)
    占位符行为 PlaceHolderBehavior 的实现以及使用
    一个简单的TabItem样式。
    WPF实现Twitter按钮效果(转)
    模仿36。杀毒~button(转)
    WPF自适应可关闭的TabControl 类似浏览器的标签页(转)
    WPF绘制简单常用的Path(转)
  • 原文地址:https://www.cnblogs.com/linlf03/p/2610594.html
Copyright © 2011-2022 走看看