Convert Zero Datetime vs Allow Zero Datetime - c#

Convert Zero Datetime=true returns DateTime.MinValue while Allow Zero Datetime return MySqlDateTime.
What else differentiate these 2 settings ?
When to use which one?
Can they be used interchangeable ?
What is pro and cons of them ?

You can refer to this point for a little clarification: http://forums.devart.com/viewtopic.php?t=21367
But it all boils down to how you want to handle zero values on DateTime, When you use Allow Zero DateTime it cannot be converted to a DateTime because it doesn't support zero value. If you use Convert Zero DateTime it will use DateTime but it will return the lower value possible for date which is DateTime.MinValue.

Related

Convert String to DateTime with Leading Zero C#

I have a datetime in the format MM/dd/yyyy:
string datetime = "05/16/2018"
Now, as per the requirement, I need to convert this string to DateTime. Whenever I do so It removes leading zero.
DateTime dt = DateTime.Parse(datetime, Globalization.CultureInfo.InvariantCulture, Globalization.DateTimeStyles.None)
Here, It gives output as 5/16/2018 but I need it as 05/16/2018. So, how do I achieve this result?
You need to understand that a DateTime is just an object that represents a point in time. It does not store information about how it is formatted. The following are the same DateTime, just in different formats:
05/16/2018
5/16/2018
because they represent the same point in time.
It's like how int behaves. It will "remove your leading zeros" as well:
int a = 05;
Console.WriteLine(a); // just prints "5".
Because 05 and 5 are the same int, as far as int is concerned.
Whether leading zeroes are added in a DateTime depends on how you format it. They only appear when you convert your DateTime to a string. 05/16/2018 and 5/16/2018 are different strings.
If you want the leading zeroes, just specify a date format like dd/MM/yyyy whenever you want to output it:
Console.WriteLine(dateTime.toString("dd/MM/yyyy")); // prints leading zeroes!
First of all your string DateTime format(MM/dd/yyyy) is confusing! It will give misleading value for a string DataTime value like "05/08/2018". So you have to use ParseExact instead of Parse as follows:
DateTime dt = DateTime.ParseExact(datetime,"MM/dd/yyyy", CultureInfo.InvariantCulture);
It gives output as 5/16/2018 but I need it as 05/16/2018. So, how do I achieve this result?
You can simply achieve this my changing you machine DateTime format to a leading zero DataTime format! bacause by default C# DateTime takes the format of the machine Datatime format.
Well, I have also checked with a Test Console Application to confirm that your converted DateTime can contain leading zero without converting it to string if your machine DateTime format contains leading zero.
"5/16/2018" is string presentation od DateTime value.
If you want to output it with leading zero, you can use custom format:
dt.ToString("dd/MM/yyyy");

Store DateTime.Now to ddMMyyyy format without string operation

Is there any possible way for saving DateTime.Now to ddMMyyyy format without using ToString(). Because whenever I use the string operation the statement is not accepted by entity framework. I need to add DateTime to DB in date format of ddMMyyyy. Is there any way??
It would be silly and counterproductive to store dates as "ddMMyyyy". First of all you'd need a varchar(8), not a DATE or DATETIME.
On top of that, how are you ever going to sort it using ORDER BY, or use BETWEEN queries, or do myDate > someValue / myDate < someValue queries? You can't with a date-string formatted like that.
Also a notation such as "ddMMyyyy" is a User Interface representation of an underlying value. Databases should almost never store User Interface representations, that is a job for the... you guessed it... User Interface.
Best to just forget about it, or else be ready to face the horrible consequences.
Change you SQL Server Database data type from "datetime" to "date" ?
From the article for the DateTime.Parse() method (MSDN Article)
So you can do:
var dateTime = DateTime.Parse("01012001");
Which will give you a DateTime typed object.
If you need to specify which date format you want to use, you would use DateTime.ParseExact (MSDN Article)
Which you would use in a situation like this (Where you are using a British style date format):
string[] formats= { "ddMMyyyy" }
var dateTime = DateTime.ParseExact("01012001", formats, new CultureInfo("en-US"), DateTimeStyles.None);
The format "ddMMyyyy" (or any other format) makes any sense only if we're talking about strings.
If you need only the day, month, and year in the program you can still use the DateTime class.
You simply ignore the other properties like minutes, hours, etc...
DateTime now = DateTime.Now;
Method(now.Day, now.Month, now.Year);
or
DateTime emptyDateTime = new DateTime();
// in this case emptyDateTime values are the following:
emptyDateTime.Year: 1
emptyDateTime.Month: 1
emptyDateTime.Day: 1
emptyDateTime.Hour: 0
emptyDateTime.Minute: 0
emptyDateTime.Second: 0

using "if" on a time literal (formatting help)

I have a time object which if null is for some reason interpreted as 00:00:00. So I need to do a test if it is null, but now I can't do that so I need the equivalent of:
if (TimeObject == 00:00:00) {...
What would the format for this if statement be?
First, C# does not support time literals, so 00:00:00 won't make sense to a standard C# compiler.
Second, in order to handle time, you will need to use DateTime or TimeSpan structures.
Third, because these are structures, they can never be null - they have a default value, but will not allow DateTime dt = null; If you want a nullable struct, use Nullable Types (thus DateTime? and TimeSpan?)

How to find the positive difference between two DateTime in C#

I've two DateTime variable regardless of which is greater than the other in time.
Datetime date1, date2;
How should I find the positive difference of both on "days" basis?
(date1-date2) might give positive/negative result but I also need the no: of days difference.
Assume both are on the same TimeZone
double days = Math.Abs((date1-date2).TotalDays);
If you want an (unsigned) integer value:
Math.Abs(date1.Subtract(date2).Days)
If you want an (unsigned) double value:
Math.Abs(date1.Subtract(date2).TotalDays)
Just use the Days property on the timespan (which is the resulting type from date1 - date2). It returns a signed int.
You could try:
Math.Abs((dateA - dateB).Days);
or if you want the result to be fractional:
Math.Abs((dateA - dateB).TotalDays);

Date compare in LINQ

i was looking for date compare in LINQ and i found a code from searching google.
var currentDate = DateTime.Now.Date;
VisitorLog log = db.Context.VisitorLogs
.Where(vl=>vl.inDate.Date == currentDate).FirstOrDefault();
i need to know the above code works fine or not.
how do i compare date in particular format like i compare date in sql server
compare(varchar(10),mydatefield,112)>=compare(varchar(10),#mydatefield,112)
so please guide me how could i compare date using linq in particular format like above one.
thanks
The thing is that you will rarely compare dates in a specific format(unless you want to compare them as strings or something). Thus is the introduction of the DateTime structure.
Anyway the most used way of comparing dates is not to check for equality but to actually check whether a date comes into a specific range. For example:
startDate <= order.OrderDate && order.OrderDate < endDate
Can this link help you?
How to compare dates in LINQ?
where t.CreateDate.Date < DateTime.Today.AddMonths(-1)
The format matters when you parse a string to a DateTime object. Once you have done that and you have two DateTime objects then comparing their Date properties is the correct thing to do. The Date properties return DateTime objects with time set to 12:00am
In order to parse the date correctly you can pass a IFormatProvider to the DateTime.Parse() method. E.g. a CultureInfo object:
DateTime d = DateTime.Parse("07/04/2011", CultureInfo.GetCultureInfo("en-us"));

Categories

Resources