I have a textbox in my project where user enters date, like ddmmyyyy. I need to convert it to dd-mm-yyyy format so that I could fetch particular data from database.
You can parse your string to DateTime first and then generate it's string representation with that format.
For example;
var s = "11062016";
var dt = DateTime.ParseExact(s, "ddMMyyyy", CultureInfo.InvariantCulture);
Console.WriteLine(dt.ToString("dd-MM-yyyy", CultureInfo.InvariantCulture));
By the way, I assume you wanna say MM instead of mm since mm specifier is for minutes but MM specifier is for months.
On the other hand, you never told the data you wanna fetch but, it is not a good idea to get DateTime values (if they are) with strings. Use DateTime values to get DateTime data from your database (which most of of RDMS supports), not strings.
Try
string str = "06112016";
DateTime date = new DateTime();
DateTime.TryParseExact(str, "ddMMyyyy", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out date);
string FormattedDate = date.ToString("MM-dd-yyyy");
Related
I want to get the last seen of user and save it to my sql database in mvc5 . I got the last seen in controller with code like this:
users.userlast=DateTime.Now;
and saved to my database in this format "2015-08-06 12:12:13.443". I want to get datetime only format day,month,year, hour and minute.
I can't use something like this,
var dateTime = DateTime.ParseExact("12/02/21 10:56:09", "yy/MM/dd HH:mm", CultureInfo.InvariantCulture);
var text = dateTime.ToString("MMM. dd, yyyy HH:mm");
It did not work because my last seen column is a datetime type not string. What should i do?
Thanks.
Edit:
Like whatsup App., i want to see only hour and minute, not seconds as last seen.
You say that you are storing as a datetime type, in which case the you shouldn't need to convert a string to a DateTime. In fact you shouldn't need to do any parsing.
When you query the database you should get a DateTime, on which you can call the ToString() you want.
to get datetime in format day, month, year, hour and minute only (without seconds, milliseconds), create a new DateTime value before save:
var dt = DateTime.Now;
users.userlast = dt.Date.AddHours(dt.Hour).AddMinutes(dt.Minute);
You don't have to worry about the format you save in the database. When you want to represent it in your specific format you can ToString it accordingly.
I want to get datetime only format day,month,year, hour and minute.
string text = dateTime.ToString("yyyy-MM-dd HH:mm",
CultureInfo.InvariantCulture);
CultureInfo.InvariantCulture is to use your specified culture regardless of the user's current culture.
You can use InvariantCulture because your user must be in a culture that uses a dot instead of a colon:
DateTime.ToString("yyyy-MM-dd HH:mm", CultureInfo.InvariantCulture);
Just do like that
var formattedDateTime = yourLastSeenDateTime.ToString("MMM. dd, yyyy HH:mm", CultureInfo.InvariantCulture);
EDIT: Try this as you mentioned in comments
DateTime dbDate = yourLastSeenDateTime;
DateTime newDateTime = new DateTime(dbDate.Year, dbDate.Month, dbDate.Day, dbDate.Hour, dbDate.Minute, 0);
I have DateTime variable in this format "dd/mm/yyyy hh:mm:ss".
I would like to convert this to "mm/dd/yyyy hh:mm:ss". (Month before day)
I tried to use DateTime.parse like this:
DateTime dt = new DateTime();
dt = dateTimePicker1.Value;
dt = DateTime.Parse(dt.ToString(), "mm/dd/yyyy", null);
but it wont work.
I also try it with DateTime.ParseExact() but still nothing happened.
Any suggestion?
A DateTime doesn't have any implicit format. It just has date and time values.
Formatting subject only applies when you try to get it's textual representation. If you wanna get string represetation of it, you can use .ToString() method like;
string s = dateTimePicker1.Value.ToString("MM/dd/yyyy hh:mm:ss",
CultureInfo.InvariantCulture);
And remember, mm specifier is for minutes, MM specifier is for months. Also hh specifier is for 12-hour clock and HH specifier is for 24-hour clock representations. You might wanna use HH instead.
I used InvariantCulture as a second parameter because / and : have special meaning as replace me current culture or supplied culture date or time separator.
string date = dateTimePicker1.Value.ToString("MM/dd/yyyy");
You can also use this below code for advance:
GregorianCalendar cal = new GregorianCalendar(); // or any type of calendar you want ...
string formatted = string.Format("{1:00}/{2:00}/{0} {3:00}:{4:00}:{5:00} ",
cal.GetYear(dt), cal.GetMonth(dt), cal.GetDayOfMonth(dt) , cal.GetHour(dt), cal.GetMinute(dt), cal.GetSecond(dt));
DateTime time = DateTime.Now;
string format = "MMM ddd d HH:mm yyyy";
Console.WriteLine(time.ToString(format));
i have a text-box in a detailview and the value of the text-box is a Date but it only shows the Month and Year and it is like this:November 2013 so i want to take this value and convert like this: 20131101. So as you can see, i would like the format to be YYYYMMDD but the day should always be 01 which is the first of the month. So how can i go from this November 2013 to this 20131101? here is my code and i know i have to convert from string to date first:
string myDate = ((TextBox)DetailView1.FindControl("InputDate")).Text.ToString();
Convert it:
TextBox txtInputDate = (TextBox)DetailView1.FindControl("InputDate");
DateTime dt = DateTime.ParseExact(txtInputDate.Text, "MMMM yyyy", CultureInfo.InvariantCulture);
then convert it to string again:
txtInputDate.Text = dt.ToString("yyyyMMdd", CultureInfo.InvariantCulture);
C# is pretty good at parsing stringy dates, you could lean on the build it parsing:
string myDateString = ((TextBox)DetailView1.FindControl("InputDate")).Text.ToString();
DateTime myDate;
if (DateTime.TryParse(myDateString, out myDate)) {
// myDate now contains a proper .NET date.
}
Now you have a proper DateTime, you can output it in any format you like.
DateTime test = DateTime.Parse("November 2013");
Console.WriteLine(test.ToString("yyyyMMdd"));
Use the DateTime.ParseExact() method, like this:
var theParsedDate = DateTime.ParseExact(myDate, "MMMM yyyy",
CultureInfo.InvariantCulture);
Now you can use the parsed date however you wish, convert it to string, send it to database, etc.
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!!
I know this question has been asked before and resolved using various methods.
I am converting a value from a string to a DateTime.
Throughout the project I have used the same CultureInfo, all strings that where converted to date where done using Convert.ToDateTime(), but now there is one text field that refuses to convert.
I have tried:
string date = "27/02/2013";
string startdated = (Convert.ToDateTime(date)).ToString("yyyy/MM/dd");
(converts to datetime and changes it back to sting in my required format. This works fine on everything else)
even
Datetime dt = Convert.toDateTime(date); doesn't work
DateTime.ParseExact(date, "yyyy/MM/dd", format); doesn't work
And all give me the same error "String was not recognized as a valid DateTime.". i receive my date value from a textbox with an ajax calender extender (CalendarExtender.Format = "dd/MM/yyyy" done for display purposes,this also works everywhere else i.e "dd/MM/yyyy" for display and "yyyy/MM/dd" for procedure) except this final value which simply will not change. Everthing is done via my machine with no external servers
Your input string is not in the same format as the format you provde DateTime.ParseExact with.
For this to work
DateTime.ParseExact(date, "yyyy/MM/dd", format);
You have to enter the date in year/month/day format. But your string is in day/month/year.
This should work better.
string date = "27/02/2013";
DateTime parsedDate = DateTime.ParseExact(date, "dd/MM/yyyy", CultureInfo.InvariantCulture);
Your date is date = "27/02/2013"; and your current format (in DateTime.ParseExact) is "yyyy/MM/dd", It should be:
"dd/MM/yyyy"
So the following code should work.
string date = "27/02/2013";
DateTime dt = DateTime.ParseExact(date, "dd/MM/yyyy", CultureInfo.InvariantCulture);
You can also use the format "d/M/yyyy" which would take care of single or double digit date/month.