DateTimeOffset creation

In this chapter you will learn:

  1. Create DateTimeOffset using the specified DateTime value.
  2. Create DateTimeOffset from ticks and offset

Create DateTimeOffset

using System;// j av a  2s .  c o m
using System.Collections.ObjectModel;

public class TimeOffsets
{
   public static void Main()
   {
        DateTime localNow = DateTime.Now;
        DateTimeOffset localOffset = new DateTimeOffset(localNow);
        Console.WriteLine(localOffset.ToString());
        
        DateTime utcNow = DateTime.UtcNow;
        DateTimeOffset utcOffset = new DateTimeOffset(utcNow);
        Console.WriteLine(utcOffset.ToString());
        
        DateTime unspecifiedNow = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Unspecified);
        DateTimeOffset unspecifiedOffset = new DateTimeOffset(unspecifiedNow);
        Console.WriteLine(unspecifiedOffset.ToString());
   }
}

The following code creates DateTimeOffset using the specified DateTime value and offset.

using System;/*from   ja va  2s  . c  o m*/
using System.Collections.ObjectModel;

public class TimeOffsets
{
   public static void Main()
   {
        DateTime localTime = new DateTime(2007, 07, 12, 06, 32, 00); 
        DateTimeOffset dateAndOffset = new DateTimeOffset(localTime, TimeZoneInfo.Local.GetUtcOffset(localTime));
        Console.WriteLine(dateAndOffset);
   }
}

The following code creates DateTimeOffset using the specified year, month, day, hour, minute, second, millisecond, and offset of a specified calendar.

using System;//  ja  va2s.c  om
using System.Globalization;
using System.Collections.ObjectModel;

public class TimeOffsets
{
    public static void Main()
    {
        CultureInfo fmt;
        int year;
        Calendar cal;
        DateTimeOffset dateInCal;

        // Instantiate DateTimeOffset with Hebrew calendar
        year = 5770;
        cal = new HebrewCalendar();
        fmt = new CultureInfo("he-IL");
        fmt.DateTimeFormat.Calendar = cal;
        dateInCal = new DateTimeOffset(year, 7, 12, 15, 30, 0, 0, cal, new TimeSpan(10, 0, 0));

        Console.WriteLine("Date in Hebrew Calendar: {0:g}", dateInCal.ToString(fmt));

        Console.WriteLine("Date in Gregorian Calendar: {0:g}", dateInCal);

    }
}

Create DateTimeOffset using the specified year, month, day, hour, minute, second, millisecond, and offset.

using System;/*j a  va 2  s.co  m*/
using System.Collections.ObjectModel;

public class TimeOffsets
{
   public static void Main()
   {
        string fmt = "dd MMM yyyy HH:mm:ss";
        DateTime thisDate = new DateTime(2010, 08, 12, 19, 00, 14, 16);
        DateTimeOffset offsetDate = new DateTimeOffset(thisDate.Year, 
                                                       thisDate.Month, 
                                                       thisDate.Day, 
                                                       thisDate.Hour,
                                                       thisDate.Minute,
                                                       thisDate.Second,
                                                       thisDate.Millisecond, 
                                                       new TimeSpan(2, 0, 0));  
        Console.WriteLine("Current time: {0}:{1}", offsetDate.ToString(fmt), offsetDate.Millisecond);
   }
}

ticks and offset

Create DateTimeOffset using the specified number of ticks and offset.

using System;/*j a  va 2  s. com*/
using System.Collections.ObjectModel;

public class TimeOffsets
{
   public static void Main()
   {

        DateTime localTime = DateTime.Now;
        DateTimeOffset nonLocalDateWithOffset = new DateTimeOffset(localTime.Ticks, new TimeSpan(2, 0, 0));
        Console.WriteLine(nonLocalDateWithOffset); 
        
        
        DateTime dateWithoutOffset = new DateTime(2007, 7, 16, 13, 32, 00);
        DateTimeOffset timeFromTicks = new DateTimeOffset(dateWithoutOffset.Ticks, new TimeSpan(-5, 0, 0));
        Console.WriteLine(timeFromTicks.ToString());
   }
}

Next chapter...

What you will learn in the next chapter:

  1. Gets hour from DateTimeOffset
  2. Gets year from DateTimeOffset
Home » C# Tutorial » Date, Time, TimeZone
Compare DateTimeOffset
DateTimeOffset creation
Date time Value from DateTimeOffset
Compare DateTime value
DateTime constant value
DateTime constructor
DateTime properties
Create TimeSpan From
TimeSpan
TimeSpan add and subtract
DateTime and TimeSpan
Compare TimeSpan
TimeSpan constant
TimeSpan Constructor
TimeSpan Duration
TimeSpan format
Negate a TimeSpan
TimeSpan operators
Parse string to TimeSpan
TimeSpan to string
TimeSpan total properties
TimeSpan propeties
Daylight saving
Time zone ID
UTC offset with TimeZone
TimeZone converting
TimeZone creation