How to convert timestamp to datetime in c#, but only want the date not included the hour minutes and seconds?
by using this code i have full datetime:
double timestamp = Convert.ToDouble(timestamp.Text);
System.DateTime dateTime = new System.DateTime(1970, 1, 1, 0, 0, 0, 0);
dateTime = dateTime.AddSeconds(timestamp);
lblDate.Text = dateTime.ToString(); //Latest Timestamp
just because I just want the date only I remove
dateTime = dateTime.AddSeconds(timestamp);
and change this line
System.DateTime dateTime = new System.DateTime(1970, 1, 1, 0, 0, 0, 0);
to:
System.DateTime dateTime = new System.DateTime(1970, 1, 1);
then the result that i got for date is not correct
If you want just date part you can use DateTime.ToShortDateString() or DateTime.ToLongDateString() method:
lblDate.Text = dateTime.ToShortDateString();
You can also use Standard or Custom DateTime format string. E.g.
lblDate.Text = dateTime.ToString("d"); // same as above
lblDate.Text = dateTime.ToString("D"); // same as ToLongDateString
lblDate.Text = dateTime.ToString("dd-MMM-yyyy");
Try This.
DateTime date = new DateTime(Convert.ToInt64(timestamp.Text));
After that convert your datetime in date format
if your datetime containing variable is dateTime
so convert its values as
string StrDate=dateTime.ToString("dd/MM/yyyy");
finaly u will get only date in "dd/MM/yyyy" format
Related
I have a little problem on a Unix Timestamp conversion in DateTime.
This is the Timestamp: 1521932400
I want convert it in DateTime, so I wrote this code:
public static DateTime UnixTimeStampToDateTime(long unixTimeStamp)
{
DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0);
dtDateTime = dtDateTime.AddSeconds(unixTimeStamp);
return dtDateTime;
}
essentially I declare a DateTime and then add to that the value of the Timestamp, so in this case I'll get: 24/03/2018 23:00:00 but I should get: 25/03/2018 00:00:00.
What I did wrong?
Ok problem solved. I need to convert the DateTime returned from the conversion in my timezone, so I use:
public static DateTime UnixTimeStampToDateTime(long unixTimeStamp)
{
DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0);
dtDateTime = dtDateTime.AddSeconds(unixTimeStamp);
//Convert in my timezone
TimeZoneInfo timeInfo = TimeZoneInfo.FindSystemTimeZoneById("Central Europe Standard Time");
DateTime userTime = TimeZoneInfo.ConvertTimeFromUtc(dtDateTime, timeInfo);
return userTime;
}
I am working on getting the date validated before we insert them in to the mySQl server. Below is what I am trying
DateTime startDate = new DateTime(2009, 12, 31);
var currentTime = DateTime.Now;
DateTime endDate = currentTime.Date;
DateTime DOB = DateTime.ParseExact(emp[i].DateOfBirth, "yyyy-MM-dd HH:mm:ss,fff", System.Globalization.CultureInfo.InvariantCulture);
if (!(DOB.Date > startDate && DOB.Date <= endDate))
{
WriteValidationFailure("Failed - DOB is Invalid");
}
Here emp[i].DateOfBirth is a string and hold the value like 01/01/2009. When I run the application it throws
You need to use one of these statements to parse your date:
DateTime DOB = DateTime.ParseExact(emp[i].DateOfBirth, "MM/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture);
or
DateTime DOB = DateTime.ParseExact(emp[i].DateOfBirth, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture);
First if your dates contains month as first part and second for days as first part.
I am looking for a elegant solution (if it exists) to achieve what I described in the title.
I saw an elegant solution for "changing the time in a DateTime object" and it is as follows:
DateTime s = ...;
TimeSpan ts = new TimeSpan(10, 30, 0);
s = s.Date + ts;
If there such a solution for changing the date in a DateTime object?
DateTime struct is designed to be immutable, so you can't change it. You can get a new one based on values from the old one, which is exactly what your solution does.
You can make it a bit more clear by using DateTime constructor which takes all date and time values: year, month, day, hour, minute and seconds.
s = new DateTime(s.Year, s.Month, s.Day, 10, 30, 0);
s = new DateTime(2014, 10, 2, s.Hour, s.Minute, s.Second);
or you can use TimeOfDay property:
s = new DateTime(2014, 10, 2) + s.TimeOfDay;
Assuming that you are going to pass a new DateTime object to update your existing object, a function like this can work.
DateTime UpdateDate(DateTime existingDate, DateTime newDate)
{
return newDate.Date + existingDate.TimeOfDay;
}
myDatetime = new DateTime(year, month, day);
You can use the DateTime constructors, so you can preserve the original time.
See this example:
var firstDate = new DateTime(2015, 01, 01, 15, 0, 0);
var newDate = DateTime.Now;
// New date, original time.
firstDate = new DateTime(newDate.Year, newDate.Month, newDate.Day, firstDate.Hour, firstDate.Minute, firstDate.Second, firstDate.Millisecond);
Try this:
DateTime s = ...;
s = new DateTime(year, month, day) + s.TimeOfDay;
That preserves the time in the original DateTime.
Try this:
DateTime now = DateTime.Now;
DateTime dateOnly = now.Date;
TimeSpan time = now.TimeOfDay;
dateOnly = DateTime.Parse("5/15/15"); // or whatever date you choose
DateTime newDateTime = dateOnly + time;
I would like to set principal.AccountExpirationDate with a dateTimePicker1
if i do this - its work:
principal.AccountExpirationDate = new DateTime(2015, 12, 12);
but when i try to use the dateTimePicker1 its not work - this is my code:
string enddate = dateTimePicker1.Value.ToString("yyyy, MM, dd");
int val = Convert.ToInt32(enddate);
principal.AccountExpirationDate = new DateTime(val);
Im getting error.
how can i covert the dateTimePicker1 to a value that i can set in the principal.AccountExpirationDate?
You cannot convert DateTime or DateTime formated string to integer but you can take dateTime.Day, dateTime.Month or dateTime.Year as an integer separately.
int val = dateTimePicker1.Value.Day;
int month = dateTimePicker1.Value.Month;
int year = dateTimePicker1.Value.Year;
principal.AccountExpirationDate = new DateTime(year, month, val);
But principal.AccountExpirationDate = dateTimePicker1.Value is better one because dateTimePicker1.Value returns DateTime
How to get the start date and end date of month in different variable. I have tried this and I get the start date but unable to find the end date
DateTime startDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).ToString("yyyy-MM-dd HH:mm:ss.fff");
DateTime endDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddDays(30).ToString("yyyy-MM-dd HH:mm:ss.fff");
This logic fails when month end date is 31 and 28 or 29. Your Help are surely appretiated.
You can calculate endDate like this:
DateTime endDate = startDate.AddMonths(1).AddDays(-1);
To get First Date
public DateTime FirstDayOfMonth(DateTime dateTime)
{
return new DateTime(dateTime.Year, dateTime.Month, 1);
}
To get Last Date
public DateTime LastDayOfMonth(DateTime dateTime)
{
DateTime firstDayOfTheMonth = new DateTime(dateTime.Year, dateTime.Month, 1);
return firstDayOfTheMonth.AddMonths(1).AddDays(-1);
}
DateTime endDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1)
.AddMonths(1).AddDays(-1);
You already had the start date :
DateTime monthStartDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
There's a method to get the number of days in a month (and Looking at the IL code, it seems that this way is more efficient than the other answers, though unless you're going to do it a billion time, I doubt there will be any difference) :
int daysInMonth = DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month);
DateTime monthEndDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, daysInMonth);
For first date:
DateTime first_date = new DateTime(DateTimePicker.Value.Year, DateTimePicker.Value.Month, 1);
For last date:
DateTime last_date = new DateTime(DateTimePicker.Value.Year, DateTimePicker.Value.Month, DateTime.DaysInMonth(DateTimePicker.Value.Year, DateTimePicker.Value.Month));