How do i assign back the DateTime.Now from a string? - c#

The code:
private void beginOperstionChecker(DateTime dt)
{
string time = Options_DB.Get_OperationLastTime();
DateTime.Now = time;
}
time now for example show the saved datetime.now could be minute ago or an hour ao.
the datetime.now is saved after my program is finished to make an operation.
dt = the current datetime now i use this method in the constructor.
What i want to do is to calculate the time that have been passed between the last saved datetime.now(time) and the current datetime.now(dt).
If the time that have been passed is 20 minutes or more enable true a button.

You cannot set DateTime.Now You need to create an instance of the DateTime object.
Then to get the difference you can say
TimeSpan diff = DateTime.Now - MyDateTime;
This has a property called TotalMinutes that you can use for your check.
if (diff.TotalMinutes >= 20)
{
//Do sommething
}
You can try this code
DateTime date;
if (DateTime.TryParse(time, out date))
{
TimeSpan diff = date - dt;
if (diff.TotalMinutes >= 20)
{
//Do sommething
}
}

Every time you run this method you need to persist the value somewhere. I'm going to call that variable _lastTime. That's going to be a DateTime. Further, you'll need a variable for the actual elapsed time between those two, we'll call that _elapsedTime. That's going to be a TimeSpan. With that in mind, consider this code:
private void beginOperstionChecker(DateTime dt)
{
string time = Options_DB.Get_OperationLastTime();
var dt = DateTime.Parse(time);
_elapsedTime = dt.Subtract(_elapsedTime);
_lastTime = dt;
}

You get an instance of a DateTime from a string using Parse
DateTime dt = DateTime.Parse(time)
and then you get get the time Now using
DateTime.UtcNow; or DateTime.Now;
and subtract one from the other and format as appropriate for you output

You can do this using TimeSpan.
you need to get the Difference in Minutes
DateTime dt1;//get your first date
TimeSpan duration = DateTime.Now - dt1;
if(duration.Minutes>20)
Button1.Enabled=true;

I think your looking for this:
private void beginOperstionChecker(DateTime dt)
{
string time = Options_DB.Get_OperationLastTime();
DateTime lastTime = DateTime.Parse(time);
if (DateTime.Now - lastTime > new TimeSpan(0, 20, 0))
{
//It's passed more than 20mins from last save.
}
}

You can check the time elapsed by using the TimeSpan class.
private void beginOperstionChecker(DateTime dt)
{
if(TimeSpan.FromMinutes(20) == DateTime.Now - dt)
{
//do your stuff here
}
}

Related

How to remove the portion other than date and time from datetime object? [duplicate]

I'm trying to compare a time stamp from an incoming request to a database stored value. SQL Server of course keeps some precision of milliseconds on the time, and when read into a .NET DateTime, it includes those milliseconds. The incoming request to the system, however, does not offer that precision, so I need to simply drop the milliseconds.
I feel like I'm missing something obvious, but I haven't found an elegant way to do it (C#).
The following will work for a DateTime that has fractional milliseconds, and also preserves the Kind property (Local, Utc or Undefined).
DateTime dateTime = ... anything ...
dateTime = new DateTime(
dateTime.Ticks - (dateTime.Ticks % TimeSpan.TicksPerSecond),
dateTime.Kind
);
or the equivalent and shorter:
dateTime = dateTime.AddTicks( - (dateTime.Ticks % TimeSpan.TicksPerSecond));
This could be generalized into an extension method:
public static DateTime Truncate(this DateTime dateTime, TimeSpan timeSpan)
{
if (timeSpan == TimeSpan.Zero) return dateTime; // Or could throw an ArgumentException
if (dateTime == DateTime.MinValue || dateTime == DateTime.MaxValue) return dateTime; // do not modify "guard" values
return dateTime.AddTicks(-(dateTime.Ticks % timeSpan.Ticks));
}
which is used as follows:
dateTime = dateTime.Truncate(TimeSpan.FromMilliseconds(1)); // Truncate to whole ms
dateTime = dateTime.Truncate(TimeSpan.FromSeconds(1)); // Truncate to whole second
dateTime = dateTime.Truncate(TimeSpan.FromMinutes(1)); // Truncate to whole minute
...
var date = DateTime.Now;
date = new DateTime(date.Year, date.Month, date.Day, date.Hour, date.Minute, date.Second, date.Kind);
Here is an extension method based on a previous answer that will let you truncate to any resolution...
Usage:
DateTime myDateSansMilliseconds = myDate.Truncate(TimeSpan.TicksPerSecond);
DateTime myDateSansSeconds = myDate.Truncate(TimeSpan.TicksPerMinute)
Class:
public static class DateTimeUtils
{
/// <summary>
/// <para>Truncates a DateTime to a specified resolution.</para>
/// <para>A convenient source for resolution is TimeSpan.TicksPerXXXX constants.</para>
/// </summary>
/// <param name="date">The DateTime object to truncate</param>
/// <param name="resolution">e.g. to round to nearest second, TimeSpan.TicksPerSecond</param>
/// <returns>Truncated DateTime</returns>
public static DateTime Truncate(this DateTime date, long resolution)
{
return new DateTime(date.Ticks - (date.Ticks % resolution), date.Kind);
}
}
DateTime d = DateTime.Now;
d = d.AddMilliseconds(-d.Millisecond);
Sometimes you want to truncate to something calendar-based, like year or month. Here's an extension method that lets you choose any resolution.
public enum DateTimeResolution
{
Year, Month, Day, Hour, Minute, Second, Millisecond, Tick
}
public static DateTime Truncate(this DateTime self, DateTimeResolution resolution = DateTimeResolution.Second)
{
switch (resolution)
{
case DateTimeResolution.Year:
return new DateTime(self.Year, 1, 1, 0, 0, 0, 0, self.Kind);
case DateTimeResolution.Month:
return new DateTime(self.Year, self.Month, 1, 0, 0, 0, self.Kind);
case DateTimeResolution.Day:
return new DateTime(self.Year, self.Month, self.Day, 0, 0, 0, self.Kind);
case DateTimeResolution.Hour:
return self.AddTicks(-(self.Ticks % TimeSpan.TicksPerHour));
case DateTimeResolution.Minute:
return self.AddTicks(-(self.Ticks % TimeSpan.TicksPerMinute));
case DateTimeResolution.Second:
return self.AddTicks(-(self.Ticks % TimeSpan.TicksPerSecond));
case DateTimeResolution.Millisecond:
return self.AddTicks(-(self.Ticks % TimeSpan.TicksPerMillisecond));
case DateTimeResolution.Tick:
return self.AddTicks(0);
default:
throw new ArgumentException("unrecognized resolution", "resolution");
}
}
Instead of dropping the milliseconds then comparing, why not compare the difference?
DateTime x; DateTime y;
bool areEqual = (x-y).TotalSeconds == 0;
or
TimeSpan precision = TimeSpan.FromSeconds(1);
bool areEqual = (x-y).Duration() < precision;
To round down to the second:
dateTime.AddTicks(-dateTime.Ticks % TimeSpan.TicksPerSecond)
Replace with TicksPerMinute to round down to the minute.
If your code is performance sensitive, be cautious about
new DateTime(date.Year, date.Month, date.Day, date.Hour, date.Minute, date.Second)
My app was spending 12% of CPU time in System.DateTime.GetDatePart.
Less obvious but more than 2 times faster :
// 10000000 runs
DateTime d = DateTime.Now;
// 484,375ms
d = new DateTime((d.Ticks / TimeSpan.TicksPerSecond) * TimeSpan.TicksPerSecond);
// 1296,875ms
d = d.AddMilliseconds(-d.Millisecond);
Not the fastest solution but simple and easy to understand:
DateTime d = DateTime.Now;
d = d.Date.AddHours(d.Hour).AddMinutes(d.Minute).AddSeconds(d.Second)
A way for easy reading is...
//Remove milliseconds
DateTime date = DateTime.Now;
date = DateTime.ParseExact(date.ToString("yyyy-MM-dd HH:mm:ss"), "yyyy-MM-dd HH:mm:ss", null);
And more...
//Remove seconds
DateTime date = DateTime.Now;
date = DateTime.ParseExact(date.ToString("yyyy-MM-dd HH:mm"), "yyyy-MM-dd HH:mm", null);
//Remove minutes
DateTime date = DateTime.Now;
date = DateTime.ParseExact(date.ToString("yyyy-MM-dd HH"), "yyyy-MM-dd HH", null);
//and go on...
I understand that it is easy to understand, but it lacks performance.
Regarding Diadistis response. This worked for me, except I had to use Floor to remove the fractional part of the division before the multiplication. So,
d = new DateTime((d.Ticks / TimeSpan.TicksPerSecond) * TimeSpan.TicksPerSecond);
becomes
d = new DateTime(Math.Floor(d.Ticks / TimeSpan.TicksPerSecond) * TimeSpan.TicksPerSecond);
I would have expected the division of two Long values to result in a Long, thus removing the decimal part, but it resolves it as a Double leaving the exact same value after the multiplication.
Eppsy
2 Extension methods for the solutions mentioned above
public static bool LiesAfterIgnoringMilliseconds(this DateTime theDate, DateTime compareDate, DateTimeKind kind)
{
DateTime thisDate = new DateTime(theDate.Year, theDate.Month, theDate.Day, theDate.Hour, theDate.Minute, theDate.Second, kind);
compareDate = new DateTime(compareDate.Year, compareDate.Month, compareDate.Day, compareDate.Hour, compareDate.Minute, compareDate.Second, kind);
return thisDate > compareDate;
}
public static bool LiesAfterOrEqualsIgnoringMilliseconds(this DateTime theDate, DateTime compareDate, DateTimeKind kind)
{
DateTime thisDate = new DateTime(theDate.Year, theDate.Month, theDate.Day, theDate.Hour, theDate.Minute, theDate.Second, kind);
compareDate = new DateTime(compareDate.Year, compareDate.Month, compareDate.Day, compareDate.Hour, compareDate.Minute, compareDate.Second, kind);
return thisDate >= compareDate;
}
usage:
bool liesAfter = myObject.DateProperty.LiesAfterOrEqualsIgnoringMilliseconds(startDateTime, DateTimeKind.Utc);
This is my version of the extension methods posted here and in similar questions. This validates the ticks value in an easy to read way and preserves the DateTimeKind of the original DateTime instance. (This has subtle but relevant side effects when storing to a database like MongoDB.)
If the true goal is to truncate a DateTime to a specified value (i.e. Hours/Minutes/Seconds/MS) I recommend implementing this extension method in your code instead. It ensures that you can only truncate to a valid precision and it preserves the important DateTimeKind metadata of your original instance:
public static DateTime Truncate(this DateTime dateTime, long ticks)
{
bool isValid = ticks == TimeSpan.TicksPerDay
|| ticks == TimeSpan.TicksPerHour
|| ticks == TimeSpan.TicksPerMinute
|| ticks == TimeSpan.TicksPerSecond
|| ticks == TimeSpan.TicksPerMillisecond;
// https://stackoverflow.com/questions/21704604/have-datetime-now-return-to-the-nearest-second
return isValid
? DateTime.SpecifyKind(
new DateTime(
dateTime.Ticks - (dateTime.Ticks % ticks)
),
dateTime.Kind
)
: throw new ArgumentException("Invalid ticks value given. Only TimeSpan tick values are allowed.");
}
Then you can use the method like this:
DateTime dateTime = DateTime.UtcNow.Truncate(TimeSpan.TicksPerMillisecond);
dateTime.Kind => DateTimeKind.Utc
DateID.Text = DateTime.Today.ToShortDateString();
Use ToShortDateString() //Date 2-02-2016
Use ToShortDateString() // Time
And By Use Of
ToLongDateString() // its show 19 February 2016.
:P
New Method
String Date = DateTime.Today.ToString("dd-MMM-yyyy");
// define String pass parameter dd-mmm-yyyy return 24-feb-2016
Or shown on textbox
txtDate.Text = DateTime.Today.ToString("dd-MMM-yyyy");
// put on PageonLoad
In my case, I was aiming to save TimeSpan from datetimePicker tool without saving the seconds and the milliseconds, and here is the solution.
First convert the datetimePicker.value to your desired format, which mine is "HH:mm" then convert it back to TimeSpan.
var datetime = datetimepicker1.Value.ToString("HH:mm");
TimeSpan timeSpan = Convert.ToDateTime(datetime).TimeOfDay;
You can simply use Split
new TimeSpan(new DateTime(1970, 1, 1).Ticks).TotalSeconds.ToString().Split('.')[0]
I know the answer is quite late, but the best way to get rid of milliseconds is
var currentDateTime = DateTime.Now.ToString("s");
Try printing the value of the variable, it will show the date time, without milliseconds.

Function to create 24h periods timestamps? c#

I have database where data is stored along timestamp entries, that are keys.
i need a function that can transform a date like DateTime.Now into a timestamp interval representing, for today as example, Aug 30th 00:00:00h to Aug 30th 23:59:59h.
How could i write that function?
What i want is to do something like
select all from table where timestamp is between a and b.(those and b values would represent the initial and terminal timestamps for a day.)
I did not create the db, i cannot modify it, i can only query it.
This will return a tuple representing the interval you probably want:
Tuple<DateTime,DateTime> GetDateInterval(DateTime datetime) {
var start = datetime.Date;
var end = start.AddDays(1).AddTicks(-1);
return new Tuple<DateTime, DateTime>(start, end);
}
Usage:
var interval = GetDateInterval(DateTime.Now);
Console.WriteLine(interval.Item1);
Console.WriteLine(interval.Item2);
Output:
30.08.2011 0:00:00
30.08.2011 23:59:59
Maybe DateTime.Now.TimeOfDay.Ticks is what you are looking for.
Hard to tell what you want, but here is a way to get the interval:
var start = DateTime.Today;
var end = start.AddDays(1).AddSeconds(-1);
It's hard to answer how you should store the values in the DB since you haven't provided enough details about the table layout.
If you have the scope of a day, you can calculate the ticks withing the day:
// ----------------------------------------------------------------------
public long GetTicksOfDay( DateTime moment )
{
return moment.Subtract( moment.Date ).Ticks;
} // GetTicksOfDay
I think what you need is a TimeSpan.
You can just substract one DateTime from another, and the result will be a TimeSpan of the difference.
I guess you might try using a structure as a TimeInterval:
public struct TimeInterval
{
private readonly DateTime _startTime;
private readonly DateTime _endTime;
public DateTime StartTime { get { return _startTime; } }
public DateTime EndTime { get { return _endTime; } }
public TimeInterval(DateTime now)
{
_startTime = now.Date;
_endTime = now.Date.AddDays(1).AddTicks(-1);
}
}
You can call it:
TimeInterval day = new TimeInterval(DateTime.Now);

DateTime.now subtraction with value from database

I need to be able to get the difference from the current date with the date from my database. If the difference is 30, I need to display a expiry date message. My code block looks as such:
var expiryDate = DateTime.Now - DateTime.Parse(user[3]);
The thing is, this returns some weird numbers which I can't seem to manage. How would I go about getting just the number of days and then check if it is 30?
Thanks for having a look guys!
DateTime dt1 = DateTime.Now;
DateTime dt2 = DateTime.Parse(user[3]);
TimeSpan ts = dt1 - dt2;
int days = ts.Days;
if (days == 30){
//do something
}
(DateTime.Now - DateTime.Parse(user[3])).TotalDays //this will give you the days.
var timespan = DateTime.Now - DateTime.Parse(user[3]);
var days = timespan.Days;
System.TimeSpan diffResult = dt1 - dt2;
if(diffResult < 0)
{
//your code
}
When you subtract two DateTime instances you get a Timespan.
Validate your value against the TotalDays property of this timespan
var expiryDate = DateTime.Now - DateTime.Parse(user[3]);
expiryDate.TotalDays > 30 // check in this fashion

How do I subtract one DateTime from another?

I have an instance of DateTime that I get from my database, I want to subtract it from DateTime.Now and find out if 4 hours were passed. How do I do that?
Also when should i use DateTime.UTCNow or DateTimeOffset
You can use the subtraction operator to get a TimeSpan:
private static readonly TimeSpan MinimumTime = TimeSpan.FromHours(4);
...
if ((dateFromDatabase - DateTime.Now) > MinimumTime)
{
...
}
As for whether you need UTCNow or Now... it will depend on what happens to time zones when you fetch the data from the database. DateTime is not terribly clear on this front :(
If you can fetch the value as a DateTimeOffset to start with, then you can use DateTimeOffset.Now instead and it should be simpler to work out any time zone issues.
DateTime.Subtract
First Google hit..
Try this:
bool fourHoursPassed = date.AddHours(4) < DateTime.Now;
or this to actually perform a subtraction:
bool fourHoursPassed = (DateTime.Now - date).TotalHours > 4;
DateTime.Subtract
or
DateTime myDateTime = someValue;
TimeSpan ts = DateTime.Now -myDateTime;
if(ts.Hours>=4)
{
doSomething();
}
Hope it helps.
DateTime dt = new DateTime(2011, 07, 10);
DateTime dob = new DateTime(1987, 07, 10);
You can simply subtract as:
TimeSpan age = dt - dob;

How to truncate milliseconds off of a .NET DateTime

I'm trying to compare a time stamp from an incoming request to a database stored value. SQL Server of course keeps some precision of milliseconds on the time, and when read into a .NET DateTime, it includes those milliseconds. The incoming request to the system, however, does not offer that precision, so I need to simply drop the milliseconds.
I feel like I'm missing something obvious, but I haven't found an elegant way to do it (C#).
The following will work for a DateTime that has fractional milliseconds, and also preserves the Kind property (Local, Utc or Undefined).
DateTime dateTime = ... anything ...
dateTime = new DateTime(
dateTime.Ticks - (dateTime.Ticks % TimeSpan.TicksPerSecond),
dateTime.Kind
);
or the equivalent and shorter:
dateTime = dateTime.AddTicks( - (dateTime.Ticks % TimeSpan.TicksPerSecond));
This could be generalized into an extension method:
public static DateTime Truncate(this DateTime dateTime, TimeSpan timeSpan)
{
if (timeSpan == TimeSpan.Zero) return dateTime; // Or could throw an ArgumentException
if (dateTime == DateTime.MinValue || dateTime == DateTime.MaxValue) return dateTime; // do not modify "guard" values
return dateTime.AddTicks(-(dateTime.Ticks % timeSpan.Ticks));
}
which is used as follows:
dateTime = dateTime.Truncate(TimeSpan.FromMilliseconds(1)); // Truncate to whole ms
dateTime = dateTime.Truncate(TimeSpan.FromSeconds(1)); // Truncate to whole second
dateTime = dateTime.Truncate(TimeSpan.FromMinutes(1)); // Truncate to whole minute
...
var date = DateTime.Now;
date = new DateTime(date.Year, date.Month, date.Day, date.Hour, date.Minute, date.Second, date.Kind);
Here is an extension method based on a previous answer that will let you truncate to any resolution...
Usage:
DateTime myDateSansMilliseconds = myDate.Truncate(TimeSpan.TicksPerSecond);
DateTime myDateSansSeconds = myDate.Truncate(TimeSpan.TicksPerMinute)
Class:
public static class DateTimeUtils
{
/// <summary>
/// <para>Truncates a DateTime to a specified resolution.</para>
/// <para>A convenient source for resolution is TimeSpan.TicksPerXXXX constants.</para>
/// </summary>
/// <param name="date">The DateTime object to truncate</param>
/// <param name="resolution">e.g. to round to nearest second, TimeSpan.TicksPerSecond</param>
/// <returns>Truncated DateTime</returns>
public static DateTime Truncate(this DateTime date, long resolution)
{
return new DateTime(date.Ticks - (date.Ticks % resolution), date.Kind);
}
}
DateTime d = DateTime.Now;
d = d.AddMilliseconds(-d.Millisecond);
Sometimes you want to truncate to something calendar-based, like year or month. Here's an extension method that lets you choose any resolution.
public enum DateTimeResolution
{
Year, Month, Day, Hour, Minute, Second, Millisecond, Tick
}
public static DateTime Truncate(this DateTime self, DateTimeResolution resolution = DateTimeResolution.Second)
{
switch (resolution)
{
case DateTimeResolution.Year:
return new DateTime(self.Year, 1, 1, 0, 0, 0, 0, self.Kind);
case DateTimeResolution.Month:
return new DateTime(self.Year, self.Month, 1, 0, 0, 0, self.Kind);
case DateTimeResolution.Day:
return new DateTime(self.Year, self.Month, self.Day, 0, 0, 0, self.Kind);
case DateTimeResolution.Hour:
return self.AddTicks(-(self.Ticks % TimeSpan.TicksPerHour));
case DateTimeResolution.Minute:
return self.AddTicks(-(self.Ticks % TimeSpan.TicksPerMinute));
case DateTimeResolution.Second:
return self.AddTicks(-(self.Ticks % TimeSpan.TicksPerSecond));
case DateTimeResolution.Millisecond:
return self.AddTicks(-(self.Ticks % TimeSpan.TicksPerMillisecond));
case DateTimeResolution.Tick:
return self.AddTicks(0);
default:
throw new ArgumentException("unrecognized resolution", "resolution");
}
}
Instead of dropping the milliseconds then comparing, why not compare the difference?
DateTime x; DateTime y;
bool areEqual = (x-y).TotalSeconds == 0;
or
TimeSpan precision = TimeSpan.FromSeconds(1);
bool areEqual = (x-y).Duration() < precision;
To round down to the second:
dateTime.AddTicks(-dateTime.Ticks % TimeSpan.TicksPerSecond)
Replace with TicksPerMinute to round down to the minute.
If your code is performance sensitive, be cautious about
new DateTime(date.Year, date.Month, date.Day, date.Hour, date.Minute, date.Second)
My app was spending 12% of CPU time in System.DateTime.GetDatePart.
Less obvious but more than 2 times faster :
// 10000000 runs
DateTime d = DateTime.Now;
// 484,375ms
d = new DateTime((d.Ticks / TimeSpan.TicksPerSecond) * TimeSpan.TicksPerSecond);
// 1296,875ms
d = d.AddMilliseconds(-d.Millisecond);
Not the fastest solution but simple and easy to understand:
DateTime d = DateTime.Now;
d = d.Date.AddHours(d.Hour).AddMinutes(d.Minute).AddSeconds(d.Second)
A way for easy reading is...
//Remove milliseconds
DateTime date = DateTime.Now;
date = DateTime.ParseExact(date.ToString("yyyy-MM-dd HH:mm:ss"), "yyyy-MM-dd HH:mm:ss", null);
And more...
//Remove seconds
DateTime date = DateTime.Now;
date = DateTime.ParseExact(date.ToString("yyyy-MM-dd HH:mm"), "yyyy-MM-dd HH:mm", null);
//Remove minutes
DateTime date = DateTime.Now;
date = DateTime.ParseExact(date.ToString("yyyy-MM-dd HH"), "yyyy-MM-dd HH", null);
//and go on...
I understand that it is easy to understand, but it lacks performance.
Regarding Diadistis response. This worked for me, except I had to use Floor to remove the fractional part of the division before the multiplication. So,
d = new DateTime((d.Ticks / TimeSpan.TicksPerSecond) * TimeSpan.TicksPerSecond);
becomes
d = new DateTime(Math.Floor(d.Ticks / TimeSpan.TicksPerSecond) * TimeSpan.TicksPerSecond);
I would have expected the division of two Long values to result in a Long, thus removing the decimal part, but it resolves it as a Double leaving the exact same value after the multiplication.
Eppsy
2 Extension methods for the solutions mentioned above
public static bool LiesAfterIgnoringMilliseconds(this DateTime theDate, DateTime compareDate, DateTimeKind kind)
{
DateTime thisDate = new DateTime(theDate.Year, theDate.Month, theDate.Day, theDate.Hour, theDate.Minute, theDate.Second, kind);
compareDate = new DateTime(compareDate.Year, compareDate.Month, compareDate.Day, compareDate.Hour, compareDate.Minute, compareDate.Second, kind);
return thisDate > compareDate;
}
public static bool LiesAfterOrEqualsIgnoringMilliseconds(this DateTime theDate, DateTime compareDate, DateTimeKind kind)
{
DateTime thisDate = new DateTime(theDate.Year, theDate.Month, theDate.Day, theDate.Hour, theDate.Minute, theDate.Second, kind);
compareDate = new DateTime(compareDate.Year, compareDate.Month, compareDate.Day, compareDate.Hour, compareDate.Minute, compareDate.Second, kind);
return thisDate >= compareDate;
}
usage:
bool liesAfter = myObject.DateProperty.LiesAfterOrEqualsIgnoringMilliseconds(startDateTime, DateTimeKind.Utc);
This is my version of the extension methods posted here and in similar questions. This validates the ticks value in an easy to read way and preserves the DateTimeKind of the original DateTime instance. (This has subtle but relevant side effects when storing to a database like MongoDB.)
If the true goal is to truncate a DateTime to a specified value (i.e. Hours/Minutes/Seconds/MS) I recommend implementing this extension method in your code instead. It ensures that you can only truncate to a valid precision and it preserves the important DateTimeKind metadata of your original instance:
public static DateTime Truncate(this DateTime dateTime, long ticks)
{
bool isValid = ticks == TimeSpan.TicksPerDay
|| ticks == TimeSpan.TicksPerHour
|| ticks == TimeSpan.TicksPerMinute
|| ticks == TimeSpan.TicksPerSecond
|| ticks == TimeSpan.TicksPerMillisecond;
// https://stackoverflow.com/questions/21704604/have-datetime-now-return-to-the-nearest-second
return isValid
? DateTime.SpecifyKind(
new DateTime(
dateTime.Ticks - (dateTime.Ticks % ticks)
),
dateTime.Kind
)
: throw new ArgumentException("Invalid ticks value given. Only TimeSpan tick values are allowed.");
}
Then you can use the method like this:
DateTime dateTime = DateTime.UtcNow.Truncate(TimeSpan.TicksPerMillisecond);
dateTime.Kind => DateTimeKind.Utc
DateID.Text = DateTime.Today.ToShortDateString();
Use ToShortDateString() //Date 2-02-2016
Use ToShortDateString() // Time
And By Use Of
ToLongDateString() // its show 19 February 2016.
:P
New Method
String Date = DateTime.Today.ToString("dd-MMM-yyyy");
// define String pass parameter dd-mmm-yyyy return 24-feb-2016
Or shown on textbox
txtDate.Text = DateTime.Today.ToString("dd-MMM-yyyy");
// put on PageonLoad
In my case, I was aiming to save TimeSpan from datetimePicker tool without saving the seconds and the milliseconds, and here is the solution.
First convert the datetimePicker.value to your desired format, which mine is "HH:mm" then convert it back to TimeSpan.
var datetime = datetimepicker1.Value.ToString("HH:mm");
TimeSpan timeSpan = Convert.ToDateTime(datetime).TimeOfDay;
You can simply use Split
new TimeSpan(new DateTime(1970, 1, 1).Ticks).TotalSeconds.ToString().Split('.')[0]
I know the answer is quite late, but the best way to get rid of milliseconds is
var currentDateTime = DateTime.Now.ToString("s");
Try printing the value of the variable, it will show the date time, without milliseconds.

Categories