Converts a DateTime to a UNIX timestamp. - CSharp System

CSharp examples for System:DateTime Unix

Description

Converts a DateTime to a UNIX timestamp.

Demo Code

/*//  w  w  w .j  a v  a 2 s.c  o  m
 * Facebook Google Analytics Tracker
 * Copyright 2010 Doug Rathbone
 * http://www.diaryofaninja.com
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
using System;

public class Main{
        /// <summary>
      /// Converts a DateTime to a UNIX timestamp.
      /// </summary>
      /// <param name="value">The value.</param>
      /// <returns></returns>
      public static int ConvertToUnixTimestamp(DateTime value)      
      {
         //create Timespan by subtracting the value provided from
         //the Unix Epoch
         TimeSpan span = (value - new DateTime(1970, 1, 1, 0, 0, 0, 0).ToLocalTime());
      
         //return the total seconds (which is a UNIX timestamp)
         return (int)span.TotalSeconds;
      }
}

Related Tutorials