Click here to Skip to main content
15,889,810 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am getting an error while converting from int to datetime and also I don't know how to convert from int to date time.

sample code:

C#
DateTime date = DateTime.Now.Date.Hour;
Posted
Updated 1-Aug-12 0:46am
v2

use TimeSpan Hour = new TimeSpan(DateTime.Now.Hour, 0, 0);
 
Share this answer
 
Why dont you use DateTime as it is. you can get the hour from it. See code below:
date.ToString("HH")
 
Share this answer
 
Try this code:
C#
long  ns = 10000000000000;
DateTime date = new DateTime(ns, DateTimeKind.Local);


hope it can help u!
 
Share this answer
 
hi madhu,

i also tried a lot but getting exception converting from int to datetime.
i used explicit typecasting.
see here,

int a= textbox1.text(if u will enter date in textbox)
DateTime dt= Convert.Toint32(a);
MesageBox.Show(""+dt);
 
Share this answer
 
For performance issues I've written new Classes for Date And Time named FriendsDate and FriendsTime:

C#
/// <summary>Light Date Class</summary>
public class FriendsDate
{
    public FriendsDate() { }
    public FriendsDate(int date) // 13901020
    {
        if (date < 10101)
            date = 10101;
        Year = date / 10000;
        date = date % 10000;
        Month = date / 100;
        Day = date % 100;
    }

    int day = 1, month = 1, year = 1300;
    public int Day
    {
        get { return day; }
        set { day = (value <= 0 || value > 31 ? 1 : value); }
    }
    public int Month
    {
        get { return month; }
        set { month = (value <= 0 || value > 12 ? 1 : value); }
    }
    public int Year
    {
        get { return year; }
        set { year = (value <= 0 || value >= 10000 ? 1 : value); }
    }
    char separator = '/';
    public char Separator
    {
        get { return separator; }
        set { separator = value; }
    }

    public int Value
    {
        get { return year * 10000 + month * 100 + day; }
    }

    public override string ToString()
    {
        string sep = separator.ToString();
        return year.ToString("0000") + sep +
                month.ToString("00") + sep +
                day.ToString("00");
    }

    public static FriendsDate Parse(string date)
    {
        int _year, _month, _day;
        int.TryParse(date.Substring(0, 4), out _year);
        int.TryParse(date.Substring(5, 2), out _month);
        int.TryParse(date.Substring(8, 2), out _day);
        return new FriendsDate() { Year = _year, Month = _month, Day = _day };
    }

    public static implicit operator int(FriendsDate date)
    {
        if (date == null)
            return 0;
        return date.year * 10000 + date.month * 100 + date.day;
    }
}

And
C#
public class FriendsTime
{
    public FriendsTime()
    {
        Hour = System.DateTime.Now.Hour;
        Minute = System.DateTime.Now.Minute;
        Second = System.DateTime.Now.Second;
    }
    public FriendsTime(int time) // 172034   17:20:34
    {
        if (time > 235959 || time < 0)
            time = 0;
        Hour = time / 10000;
        time = time % 10000;
        Minute = time / 100;
        Second = time % 100;
    }

    int second = 1, minute = 1, hour = 1300;
    public int Second
    {
        get { return second; }
        set { second = (value < 0 || value > 59 ? 0 : value); }
    }
    public int Minute
    {
        get { return minute; }
        set { minute = (value < 0 || value > 59 ? 0 : value); }
    }
    public int Hour
    {
        get { return hour; }
        set { hour = (value < 0 || value > 23 ? 0 : value); }
    }

    public int Value
    {
        get { return hour * 10000 + minute * 100 + second; }
    }

    public override string ToString()
    {
        return hour.ToString("00") + ":" + 
                minute.ToString("00") + ":" + 
                second.ToString("00");
    }
}


And Usage:
C#
FriendsDate d = FriendsDate.Parse("2010-12-05");
int intValue = d.Value;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900