i have a column on my table "AbsenteeismDate" (type=date) when i want to get rows contain a date it return 0 row.
this is my C# code :
DateTime ClassDate = DateTime.Parse(lblDate.Content.ToString());
var Abs = dbs.GETAbsenteeisms.Where(a => a.AbsenteeismDate == ClassDate ).ToList();
i checked it there is a problem :
"AbsenteeismDate" on database and "ClassDate" aren't equal.
eg.
AbsenteeismDate=1396-05-31
and
ClassDate=1396-05-31 12:00:00 AM
how can i get Date without Time with DateTime type because AbsenteeismDate's type is date on my database.
sorry i can't speak English very well.
A DateTime always has a date and time portion, but if you want to get a DateTime of that date and the time value set to 12:00:00 midnight (00:00:00) use DateTime.Date:
var Abs = dbs.GETAbsenteeisms
.Where(a => a.AbsenteeismDate == ClassDate.Date)
.ToList();
You do just define date time but without the time:
string date = System.DateTime.Now.ToString("yyyy-MM-dd");
Use the Date property:
DateTime ClassDate = DateTime.Parse(lblDate.Content.ToString());
var date = ClassDate.Date;
Otherwise you will have to convert to string as follow
var date=ClassDate.ToString("yyyy-MM-dd");
This works for me:
string a = DateTime.Now.ToShortDateString();
ToShortDateString() converts the value of the current System.DateTime object to equivalent short date string representation. This means the Console output woulf be e.g. 01.01.2017
You can try like this byt setting the date format:
DateTime ClassDate = DateTime.Parse(lblDate.Content.ToString(("yyyy-MM-dd"));
var Abs = dbs.GETAbsenteeisms.Where(a => a.AbsenteeismDate == ClassDate ).ToList();
Related
How to check dateTime greater than with below format date Time.
In "CreatedDate" column have below format values.
Created Date
2020-03-01 10:59:50.250
2020-03-01 10:40:39.610
2020-03-01 10:39:18.087
2020-02-29 07:39:18.087
public IQueryable<UserProfile> GetTopUserProfiles(System.DateTime startDateTime)
{
return GetDbContext(toUpdate).Get<UserProfile>( w =>w.CreatedDate >= startDateTime);
}
var dtstart = DateTime.Now.ToString("yyyy-MM-dd");
statDateTime = Convert.ToDateTime(dtstart).AddHours(8); //add 8 hours to current date.
//start DateTime value: 3/1/2020 8:00:00 AM
var count = _userProfileRepository.Value.GetTopUserProfiles(startDate).Count();
count is returning 0.
Expected count is 3
Basically, You no need to convert to yyyy-MM-dd.
You should consider whether the server time value between .NET back-end and SQL Database is the same or not.
Try below code
return GetDbContext(toUpdate).Get<UserProfile>( w => DateTime.Compare(w.CreatedDate,startDateTime) > 0);
All of my friend.
I want to convert informal string to dateTime in c#. Here my string value is "01042016".How can convert? can i need another step to change DateTime.
This is my code:
string FinancialYear = "01042016-31032017";
string[] splitDate = FinancialYear.Split('-');
DateTime startDate = Convert.ToDateTime(splitDate[0].ToString(),"dd/MM/yyyy"));
As we can see that the input date will be in the format ddMMyyyy so here the best option for converting the input to DateTime object is DateTime.TryParseExact the code for this will be :
string FinancialYear = "01042016-31032017";
string[] splitDate = FinancialYear.Split('-');
DateTime startDate ;
if(DateTime.TryParseExact(splitDate[0],"ddMMyyyy",CultureInfo.InvariantCulture,DateTimeStyles.None,out startDate))
{
// Proceed with the startDate it will have the required date
}
else
// Show failure message
This will create an Enumerable where index 0 is the first date and index 1 is the second date.
string FinancialYear = "01042016-31032017";
var dateRange = FinancialYear.Split('-')
.Select(d => DateTime.ParseExact(d, "ddMMyyyy", CultureInfo.InvariantCulture);
If you are not sure of the format your best bet is using DateTime.Parse() or DateTime.TryParse()
You are not 100% guaranteed that the date will be parsed correctly, especially in cases where the day and month numbers could be in the wrong order.
It is best to specify a required date format if you can so you can be sure the date was parsed correctly.
if you string is in static format, you can convert it by reconvert it to valid string format first such as
string validstring = splitDate[0].ToString().Substring(4,4)+"-"+splitDate[0].ToString().Substring(2,2) +"-"+ splitDate[0].ToString().Substring(0,2);
DateTime startDate = Convert.ToDateTime(validstring,"dd/MM/yyyy"));
I have data Table with four columns: Date, isin, Price and State.
I need to get minimum and maximum date from the Date column.
I'm using this code and it works fine with one file however with other file it shows me wrong maximum date.
DateTime stDate = Convert.ToDateTime((excleTable.Compute("min(date)", string.Empty)));
DateTime eDate = Convert.ToDateTime((excleTable.Compute("max(date)", string.Empty)));
For example on first row i have 02/27/2015 and on last row i have 03/31/2015 but it reads only till 03/09/2015 which is incorrect.
Any ideas what should i do ?
Looks like your column type isn't DateTime. If you can fix that than make sure that your DataTable has DateTime type for column date, otherwise you can use LINQ's Max and Min and convert your column to DateTime like:
DateTime stDate = dt.AsEnumerable()
.Max(r => Convert.ToDateTime(r.Field<string>("date")));
DateTime eDate = dt.AsEnumerable()
.Min(r => Convert.ToDateTime(r.Field<string>("date")));
You may have to use DateTime.ParseExact for converting to DateTime, if your string values doesn't correspond to default/available DateTime formats, like:
DateTime stDate = dt.AsEnumerable()
.Max(r => DateTime.ParseExact(r.Field<string>("date"),
"MM/dd/yyyy",
CultureInfo.InvariantCulture));
I am trying to format date in a specific order
Time = DateTime.Parse(p.Time.ToString("dd-MM-yyyy HH:mm:ss"))
Data type of Time is DateTime
But i am getting this error:
No overload for method "ToString" takes 1 arguments.
p is the object of the table from which i am getting Time.
List<ProductImageMapWrapper> lstpm = new List<ProductImageMapWrapper>();
lstpm = _db.ProductImageMaps.Where(i => i.ClientId == null && i.BrandId == null).Select(p => new ProductImageMapWrapper
{
Time= // Problem here
}
Now, I tried using it this way
Time = DateTime.Parse(string.Format("{dd-MM-yyyy HH:mm:ss}", p.Time))
but then i got this error:
LINQ to Entities does not recognize the method System.DateTime Parse(System.String) method, and this method cannot be translated into a store expression.
String Time = Convert.ToDateTime(p.Time).ToString("dd-MM-yyyy HH:mm:ss");
It looks to me like the Time property of both types (ProductImageMap and ProductImageMapWrapper) is a DateTime. If that is true, then you should use Time = p.Time
There's a common misconception that a DateTime value somehow has a format. Actually, you apply a given format when you convert the DateTime value into a string. To copy a DateTime value from one place to another, just assign it.
parenthesis are in the wrong place. You cannot parse it as that format. You have to parse P, then format as the string.
DateTime.Parse(System.DateTime.Now).ToString("dd-MM-yyyy HH:mm:ss")
Here is the example how to parse date from string and you can correct this for your structure to work:
string p = "21-11-2013 11:12:13";
DateTime time = DateTime.ParseExact(p, "dd-MM-yyyy HH:mm:ss", System.Globalization.CultureInfo.CurrentCulture);
Considering p.Time as string value in the date format you suggested, I think you want to parse string to DateTime as,
CultureInfo provider = CultureInfo.InvariantCulture;
string format = "dd-MM-yyyy HH:mm:ss"; //This should be format that you get in string
List<ProductImageMapWrapper> lstpm = new List<ProductImageMapWrapper>();
lstpm = _db.ProductImageMaps.Where(i => i.ClientId == null && i.BrandId == null).Select(p => new ProductImageMapWrapper
{
Time = DateTime.ParseExact(p.Time, format, provider)
});
Might Help
var selectQuery=from add in db.address
select add.myDate.toString("{0:dddd, MMMM d, yyyy}");
selectQuery.Distinct();
Normal Convers.
DateTime time = DateTime.Now; // Use current time
string format = "MMM ddd d HH:mm yyyy"; // Use this format
Console.WriteLine(time.ToString(format));
1.MMM display three-letter month
2.ddd display three-letter day of the WEEK
3.d display day of the MONTH
4.HH display two-digit hours on 24-hour scale
5.mm display two-digit minutes
6.yyyy displayfour-digit year
You want to use DateTime.ToString(format) not Nullable.ToString(no
overload):
DateTime? myDate = form.dteStartDate;
string sqlFormattedDate = myDate.Value.ToString("yyyy-MM-dd HH:mm:ss");
Of course this doesn't handle the case that there is no value. Perhaps something like this:
string sqlFormattedDate = myDate.HasValue
? myDate.Value.ToString("yyyy-MM-dd HH:mm:ss")
: "<not available>";
How can I remove time from datetime and store the output in datetime format? I do not want to show the time.
Say I have
string d = "2/27/2013 4:18:53 PM"
How can I store the output in a DateTime variable with only the date and not time.
I can use ToShortDateString() but then it return a string and not datetime.
My ultimate goal is to sort the date column chronologically which can only be done if all the entries are in datetime format and not string.
The Date property of the DateTime struct will give you a date but it will always have a time component that represents midnight ("00:00:00"). If you're starting with a string, you might be able to work with something like this:
DateTime d = DateTime.Parse("2/27/2013 4:18:53 PM").Date; // 2/27/2013 12:00:00 AM
Just make sure you perform your comparison on DateTime objects (i.e. omit all usages of ToString()).
Alternatively, you can format your date in the "sortable" time format:
string d = DateTime.Parse("2/27/2013 4:18:53 PM").ToString("s");
or
string d = yourDateTime.ToString("s");
For the above case d would be 2013-02-27T16:18:53. When sorted alphabetically, the strings will be in chronological order.
DateTime dt = DateTime.now(); (To get Any Date)
string datewithMonth= dt.ToString("MMMM dd, yyyy");
string onlyDate = DateTime.Parse(datewithMonth).ToShortDateString();
Here we get result as 1/1/2014. So that we can perform select operation on sql like this:
searchQuery = "select * from YourTableName where ColumnName = ' " + onlyDate + " ' ";
What about removing the time in culture:-
var submissionDateData = DateTime.Now.ToShortDateString();
var submissionDate = DateTime.Parse(submissionDateData, CultureInfo.CreateSpecificCulture("en-GB"));
First line gives 02/11/2015
Second line gives 11/02/2015 12:00:00 AM
Do you have to do a string split on this or is there a way to get rid of the time?
As easy as this:
d.ToString("mm/dd/yyyy");
DateTime date = DateTime.Parse(d);