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>";
Related
I have a date in this format "2017-03-29" and time like "09:30", How do I conver toDatetime.
Following is how I have
string date = "2017-03-29";
string time = "09:30"
I need to convert this to DateTime in c#.
I also need to compare this converted DateTime with current dateTime, I will be using this in comparison in Linq
Use DateTime.ParseExact. Also your problem statement and code shown have nothing to do with Linq. The code below assumes the hours are in 24 hour format, adjust accordingly if that is not the case and provide an am/pm flag.
string date = "2017-03-29";
string time = "09:30";
var dateTime = DateTime.ParseExact(date+time, "yyyy-MM-ddHH:mm", null);
I would say the same as #Sam, but I don't have enough reputation to comment.
string date = "2017-03-29";
string time = "09:30";
string dateTimeString = string.Format("{0} {1}", date, time);
DateTime dateTime = DateTime.ParseExact(dateTimeString, "yyyy-MM-dd HH:mm", CultureInfo.InvariantCulture);
Note that the Kind of the resulting DateTime is DateTimeKind.Unspecified. Convert it as necessary.
Using the variables provided:
string dateTime = date + " " + time;
DateTime d = Convert.ToDateTime(dateTime);
I have a string ("CompletionDate") which contains the value "2/28/2017 5:24:00 PM"
Now I have 2 variables (EDate and ETime). I want to assign the Date to EDate (i.e 2/28/2017) and Time to ETime (i.e. 5:24:00 PM).
How can I split the Date and Time from a single string.
Kindly Help.
My approach right now is like :
string CompletionDate = string.Empty;
string ProjectEDate = string.Empty;
string ProjectETime = string.Empty;
CompletionDate = "2017-03-29 12:58:00";
DateTime dt = DateTime.ParseExact(CompletionDate, "yyyy-MM-dd", CultureInfo.CreateSpecificCulture("en-us"));
DateTime dt1 = DateTime.ParseExact(CompletionDate, "HH:mm:ss", CultureInfo.CreateSpecificCulture("en-us"));
var ProjectEDate = dt.ToString();
var ProjectETime = dt1.ToString();
But its throwing exception that string is not in correct format. Kindly help
#Chris pointed one of your problems, but you have one more. You are passing full date time string and trying to treat it as date or time only, which is not true. Instead I suggest you to parse DateTime object with both date and time, and then take whatever you need from parsed object:
CultureInfo enUS = CultureInfo.CreateSpecificCulture("en-us");
DateTime dt = DateTime.ParseExact(CompletionDate, "yyyy-MM-dd HH:mm:ss", enUS);
var ProjectEDate = dt.Date.ToString();
var ProjectETime = dt.TimeOfDay.ToString();
You need to specify the full format as same as the input string to parse method.
DateTime dt = DateTime.ParseExact(CompletionDate, "yyyy-MM-dd HH:mm:ss", System.Globalization.CultureInfo.CreateSpecificCulture("en-us"));
To get results you can use below methods available by default in DateTime.
dt.ToShortTimeString()
"12:58 PM"
dt.ToLongTimeString()
"12:58:00 PM"
dt.ToLongDateString()
"Wednesday, March 29, 2017"
dt.ToShortDateString()
"3/29/2017"
Or you can specify the format to ToString method.
dt.ToString("yyyy-MM-dd")
"2017-03-29"
dt.ToString("HH:mm:ss")
"12:58:00"
DateTime.ParseExact(CompletionDate, "yyy-MM-dd", ...
You are missing 4th 'y' in date format string:
"yyyy-MM-dd"
^
here
and:
String was not recognized as a valid DateTime " format dd/MM/yyyy"
Why do you parse into DateTime and then convert to a string using ToString again? CouldnĀ“t you just simply use String.Split when all you want is to split the time from the day and you know the exact format?
var CompletionDate = "2017-03-29 12:58:00";
var tmp = CompletionDate.Split(' ');
var ProjectEDate = tmp[0];
var ProjectETime = tmp[1];
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"));
In a variable of DateTime typeI have this value = {30/07/2014 0:00:00}
I want only the date:
var aux = pedido.ord_cus_deliv_date.ToString().Split(' ')[0];
with it I obtain 30/04/2014 correctly
but when I want to convert in MM/dd/yyyy using:
var aux2 = DateTime.ParseExact(aux, "MM/dd/yyyy", null);
I have this error:
the string is represents one DateTime not admited in the GregorianCalendar
Why I have this error in aux2?
The problem is your locale setting. Calling ToString() without parameters on a date value produces a string with the pattern day,month,year arranged differently between locales. (And I suppose that you get a string arranged with Day,Separator, Month, Separator, Year).
Passing that string to DateTime.ParseExact with a specific pattern (MM/dd/yyyy) requires the string to be in the exact pattern required Month, Day, Year for your example.
You could force the invariant culture in your conversion with
var aux = pedido.ord_cus_deliv_date.ToString(CultureInfo.InvariantCulture).Split(' ')[0];
this produces a string with the pattern required by the subsequent ParseExact mask
However it is not clear why you need these conversions. A date is not a string and you simply keep it as a date and use the conversion only when you need to represent it somewhere (display, print etc...)
Console.WriteLine("Date is:" + pedido.ord_cus_deliv_date.ToString("MM/dd/yyyy"));
When you call below :
var aux = pedido.ord_cus_deliv_date.ToString().Split(' ')[0];
This gives you code "07-30-2014" and not "07/30/2014" and that's generate the error while conversion. So to get "07/30/2014", you have to write
var aux = pedido.ord_cus_deliv_date.ToString(CultureInfo.InvariantCulture).Split(' ')[0];
Below is overall code for you:
DateTime value = DateTime.Parse("30/07/2014 0:00:00"); //your date time value
var aux = value.ToString(CultureInfo.InvariantCulture).Split(' ')[0];
DateTime dt = DateTime.ParseExact(aux, "MM/dd/yyyy", CultureInfo.InvariantCulture);
var aux2 = dt.ToString(CultureInfo.InvariantCulture).Split(' ')[0]);
I hope this will help you
Regards,
Sandeep
i have textbox that accepts time format like this 12:40 PM but would like to convert it into time format like this 12:40:00 basically without the PM or AM. Here is what i have so far:
string StartTime = ((TextBox)TestDV.FindControl("txtBST")).Text.ToString();
thanks
One option would be to parse into a DateTime and then back to a string:
string s = "12:40 PM";
DateTime dt = DateTime.Parse(s);
string s2 = dt.ToString("HH:mm:ss"); // 12:40:00
Be aware, however, that most operations work better with a DateTime versus a string representation of a DateTime.
First you should parse it to a DateTime, then format it. It sounds like your input format is something like hh:mm tt and your output format is HH:mm:ss. So, you'd have:
string input = "12:40 PM"
DateTime dateTime = DateTime.ParseExact(input, "hh:mm tt",
CultureInfo.InvariantCulture);
string output = dateTime.ToString("HH:mm:ss", CultureInfo.InvariantCulture);
Note that:
I've used DateTime.ParseExact which will throw an exception if the parsing fails; you may want to use DateTime.TryParseExact (it depends on your situation)
I've used the invariant culture for both operations here. I don't know whether or not that's correct for your scenario.
I've used hh:mm, but you might want h:mm... would you expect "1 PM" or "01 PM"?
You don't parse seconds, so that part will always be 0... is that okay?
Since you are bringing it in as a string this is actually kind of easy.
string StartTime = ((TextBox)TestDV.FindControl("txtBST")).Text.ToString();
DateTime dt = new DateTime();
try { dt = Convert.ToDateTime(StartTime); }
catch(FormatException) { dt = Convert.ToDateTime("12:00 AM"); }
StartTime = dt.ToString("HH:mm");
So you bring in your string, and convert it to a date. if the input is not a valid date, this will default it to 00:00. Either way, it gives you a string and a DateTime object to work with depending on what else you need to do. Both represent the same value, but the string will be in 24-Hour format.
Cheers!!