Contains helper for converting to and from the date formats : Date Time Format « Development Class « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Development Class » Date Time FormatScreenshots 
Contains helper for converting to and from the date formats
 
//http://facebooktoolkit.codeplex.com/
//http://facebooktoolkit.codeplex.com/license
using System;

namespace Facebook.Utility
{
    ///<summary>
    /// Contains helper for converting to and from the date formats provided by facebook
    ///</summary>
    public static class DateHelper
    {
        ///<summary>
        /// Returns a datetime corresponding to 1/1/1970
        ///</summary>
        public static DateTime BaseUTCDateTime
        {
            get return new DateTime(197011000)}
        }

        ///<summary>
        /// Event dates are stored by assuming the time which the user input was in Pacific
        /// time (PST or PDT, depending on the date), converting that to UTC, and then
        /// converting that to Unix epoch time. This algorithm reverses that process.
        ///</summary>
        public static DateTime ConvertUnixTimeToDateTime(long secondsSinceEpoch)
        {
            DateTime utcDateTime = BaseUTCDateTime.AddSeconds(secondsSinceEpoch);
            int pacificZoneOffset = utcDateTime.IsDaylightSavingTime() ? -: -8;
            return utcDateTime.AddHours(pacificZoneOffset);
        }

        /// <summary>
        /// Convert datetime to UTC time, as understood by Facebook.
        /// </summary>
        /// <param name="dateToConvert">The date that we need to pass to the api.</param>
        /// <returns>The number of seconds since Jan 1, 1970.</returns>
        public static long ConvertDateToFacebookDate(DateTime dateToConvert)
        {
            return (long)((dateToConvert - BaseUTCDateTime).TotalSeconds);
        }

        /// <summary>
        /// Convert UTC time, as returned by Facebook, to localtime.
        /// </summary>
        /// <param name="secondsSinceEpoch">The number of seconds since Jan 1, 1970.</param>
        /// <returns>Local time.</returns>
        internal static DateTime ConvertDoubleToDate(double secondsSinceEpoch)
        {
#if !SILVERLIGHT
            return TimeZone.CurrentTimeZone.ToLocalTime(BaseUTCDateTime.AddSeconds(secondsSinceEpoch));
#else
            return TimeZoneInfo.ConvertTime(BaseUTCDateTime.AddSeconds(secondsSinceEpoch), TimeZoneInfo.Local);
#endif
        }

        //Event dates are stored by assuming the time which the user input was in Pacific
        // time (PST or PDT, depending on the date), converting that to UTC, and then
        // converting that to Unix epoch time. This algorithm reverses that process.
        internal static DateTime ConvertDoubleToEventDate(double secondsSinceEpoch)
        {
            DateTime utcDateTime = BaseUTCDateTime.AddSeconds(secondsSinceEpoch);
            int pacificZoneOffset = utcDateTime.IsDaylightSavingTime() ? -: -8;
            return utcDateTime.AddHours(pacificZoneOffset);
        }

        /// <summary>
        /// Convert datetime to UTC time, as understood by Facebook.
        /// </summary>
        /// <param name="dateToConvert">The date that we need to pass to the api.</param>
        /// <returns>The number of seconds since Jan 1, 1970.</returns>
        internal static double? ConvertDateToDouble(DateTime? dateToConvert)
        {
            return dateToConvert != null new double?((dateToConvert.Value - BaseUTCDateTime).TotalSecondsnull;
        }
    }
}

   
  
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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.