I have a string like
string startdatestring = "2017-03-10T07:19:28.907";
DateTime firstdate = DateTime.ParseExact(startdatestring,
"yyyy/MM/dd hh:mm:ss",
CultureInfo.InvariantCulture);
var firstDateString = firstdate.ToString("yyyy-MM-dd");
want the output only 2017-03-10
Modify your code like below
string startdatestring = "2017-03-10T07:19:28.907";
//1st way - Convert.ToDatetime works properly
//DateTime firstdate = Convert.ToDateTime(startdatestring);
//2nd way - DateTime.Parse also works perfect
//DateTime firstdate = DateTime.Parse(startdatestring);
//3rd way - DateTime.ParseExact also works perfect
DateTime firstdate = DateTime.ParseExact(startdatestring,
"yyyy-MM-ddTHH:mm:ss.fff",
CultureInfo.InvariantCulture);
var firstDateString = firstdate.ToString("yyyy-MM-dd");
Console.WriteLine(firstDateString);
OUTPUT
2017-03-10
Your input string "2017-03-10T07:19:28.907" doesn't match the format string "yyyy/MM/dd hh:mm:ss".
Considering your input is already in a standard format, you can use DateTime.Parse:
var firstdate = DateTime.Parse(startdatestring);
DateTime firstdate = DateTime.ParseExact(startdatestring,
"yyyy-MM-dd hh:mm:ss",
CultureInfo.InvariantCulture);
it's yyyy-MM-dd instead of yyyy/MM/dd
;)
Change you code as below
DateTime firstdate = DateTime.ParseExact(startdatestring,
"yyyy-MM-ddTHH:mm:ss.fff",
CultureInfo.InvariantCulture);
Do something like this
string startdatestring = "2017-03-10T07:19:28.907";
string s = startdatestring.Substring(0, 9);
Related
I have a date/time return from a C# method is in string,
string dateTime = "2018-6-18 20:50:35"
Now I would like to convert this into another string representation like,
string convertDT = "2018-6-18 08:50:35 PM"
Is this possible?
Seems like I can do something like,
var formattedTime = dateTime.ToString("h:mm tt", CultureInfo.InvariantCulture);
but not working. Suggestion please!
Just parse the string into a new DateTime object and then call ToString() with the right formats:
string dateTime = "2018-6-18 20:50:35";
DateTime parsedDateTime;
if(DateTime.TryParse(dateTime, out parsedDateTime))
{
return parsedDateTime.ToString("yyyy-M-d hh:mm tt");
}
The benefit of my answer is that it contains validation (DateTime.TryParse()), it results in a couple extra lines of code but you can now accept all input and not worry about an exception being thrown.
Even better would be to refactor this logic into its own method that you can re-use:
public static bool TryChangeDateTimeFormat(string inputDateString, string outputFormat, out string outputDateString)
{
DateTime parsedDateTime;
if(DateTime.TryParse(inputDateString, out parsedDateTime))
{
outputDateString = parsedDateTime.ToString(outputFormat);
return true;
}
outputDateString = string.Empty;
return false;
}
This returns a bool of whether or not the conversion was successful and the out variable will be modified depending on the result.
Fiddle here
Without adding any validation,
var string24h = "2018-6-18 20:50:35";
var dateTime = DateTime.Parse(string24h);
var formattedTime = dateTime.ToString("h:mm tt", CultureInfo.InvariantCulture);
Use DateTime.ParseExact and then ToString
Sure, you can use the DateTime class to parse the original string and then output a differently formatted string for the same date:
string result = DateTime.Parse(dateTime).ToString("h:mm tt", CultureInfo.InvariantCulture);
var dateTime = "2018-6-18 20:50:35";
var dt = Convert.ToDateTime(dateTime);
var amPmDateTime = dt.ToString(#"yyyy-MM-dd hh:mm:ss tt", CultureInfo.InvariantCulture);
To give you exactly your format you would use
string convertDT = DateTime.Parse(dateTime).ToString("yyyy-MM-dd hh:mm:ss tt");
You can change the format between the quotes however you would like. For example yyyy/MM/dd or something. Just remember MM is 2 spots for months and mm is 2 spots for minutes.
So if you put
string convertDT = DateTime.Parse(dateTime).ToString("yyyy-mm-dd hh:mm:ss tt");
You are going to get year - minutes - days.
I am trying to get the date (& datetime) from the url and then convert it into its proper format before storing it in the db.
var reqDate = Request.QueryString["StartDate"];
//at this point I have reqDate: 05/15/2018 00:00:00
reqDate = reqDate.Substring(0, reqDate.IndexOf(" ") + 1);
//after stripping off the time part I have: 05/15/2018
timingRequest.ReqDate = DateTime.ParseExact(reqDate, "MM/dd/yyyy", CultureInfo.InvariantCulture);
//but this throws the exception
URL:
Same is the case with startDateTime
var reqDateTime = Request.QueryString["startDateTime"];
timingRequest.IgnoreEntry = DateTime.ParseExact(reqDateTime, "dd/MM/yyyy hh:mm tt", CultureInfo.InvariantCulture);
In your first scenario, No need to add +1 after reading indexOf(" "). +1 adding extra space to date
//Lets take date in string is "05/15/2018 00:00:00"
Console.WriteLine(s.Substring(0, reqDate.IndexOf(" ")+1)); /*This will print "05/15/2018 " WITH EXTRA SPACE*/
Correct way is s.Substring(0, s.IndexOf(" "))
In second scenario, use date format like HH:mm:ss instead of HH:mm tt
//Here use "hh:mm:ss" instead of "hh:mm tt"
DateTime dateTime = DateTime.ParseExact(reqDateTime, "dd/MM/yyyy hh:mm:ss", CultureInfo.InvariantCulture);
Elegant approach would be:
#Credit Stephen Muecke
After looking at your URL, you can write a method having parameters like,
public ActionResult Create(int empId, int attID, DateTime startDate, DateTime startDateTime)
{
/*Do your work here, DefaultModelBinder will take care of parameters*/
}
First Example fix:
var reqDate = Request.QueryString["StartDate"];
reqDate = reqDate.Substring(0, reqDate.IndexOf(" "));
timingRequest.ReqDate = DateTime.ParseExact(reqDate, "MM/dd/yyyy", CultureInfo.InvariantCulture);
Second Example Fix:
var reqDateTime = Request.QueryString["startDateTime"];
timingRequest.IgnoreEntry = DateTime.ParseExact(reqDateTime, "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture);
I tried the following but couldn't get it
string s= "2015-FEB-17";
//I want it to be converted to date format as
date = "20150217"
//I tried doing as follows but didn't work
var myDate = DateTime.ParseExact(dt, "yyyy-MMM-dd", CultureInfo.InvariantCulture);
why not shorten it since you are using the .ToString("yyyyMMdd") you are dropping the time portion of the new value
string sDateStr = "2015-FEB-17";
var newDateFrmt = Convert.ToDateTime(sDateStr).ToString("yyyyMMdd");
20150217 becomes the expected answer based on the format..
not to be redundant this approach can also be taken
string sDateStr = "2015-FEB-17";
var someDate = DateTime.ParseExact(sDateStr, "yyyy-MMM-dd", CultureInfo.InvariantCulture);
var newDateFrmt = someDate.ToString("yyyyMMdd");
ParseExact turns the string into a DateTime. You then need to format the DateTime as a string.
string s= "2015-FEB-17";
DateTime myDate = DateTime.ParseExact(s, "yyyy-MMM-dd", CultureInfo.InvariantCulture);
string result = myDate.ToString("yyyyMMdd"); // now it's "20150217"
Also, you were missing dashes in the ParseExact format string.
If your application only handles US-style dates with no internationalization, it's best to specify CultureInfo.InvariantCulture.
Try
var myDate = DateTime.ParseExact(s, "yyyy-MMM-dd", CultureInfo.InvariantCulture);
var q = myDate.ToString("yyyyMMdd");
or just
var q = DateTime.ParseExact(s, "yyyy-MMM-dd", CultureInfo.InvariantCulture)
.ToString("yyyyMMdd");
I need to use
SqlDateTime.Parse(val)
where val is a string such as " 23.3.1992 00:00:00 ".
The string is in European format, that is, day precedes month. However Parse wants "American" format. How I can tell it to use particular datetime format / locale?
Thanks in advance!
Try this:
string val = "23.12.1992 00:00:00";
// Parse exactly from your input string to the native date format.
DateTime dt = DateTime.ParseExact(val, "dd.M.yyyy hh:mm:ss", null);
// Part to SqlDateTime then
System.Data.SqlTypes.SqlDateTime dtSql = System.Data.SqlTypes.SqlDateTime.Parse(dt.ToString("yyyy/MM/dd"));
This could be done in one statement, but just separated for illustration.
Have you tried DateTime instead of SQLDateTime
DateTime d = DateTime.Parse(val);
String s = d.ToString(CultureInfo.CreateSpecificCulture("en-US"));
Can you try this ?
string valInEuropean = "23.3.1992 00:00:00";
DateTime dateInEuropean = DateTime.Parse(valInEuropean);
string valInAmerican = dateInEuropean.ToString("yyyy-MM-dd HH:mm:ww");
For converting a string to datetime object when the format is known(in this case )
use
DateTime dwweek = DateTime.ParseExact("23.3.1992 00:00:00", "dd.MM.yyyy hh:mm:ss", System.Globalization.CultureInfo.InvariantCulture);
I have to convert a date string to a DateTime object as follows
tmpdate = "27-Apr 14:53";
TheDate = DateTime.ParseExact(tmpdate, "DD-MMM HH:mm", CultureInfo.InvariantCulture);
I keep getting exceptions about the string not being a valid date time. I've tried adding in the year as well with no luck. Any suggestions?
Try dd-MMM HH:mm - note lowercased dd.
try with little d :
TheDate = DateTime.ParseExact(tmpdate, "dd-MMM HH:mm", CultureInfo.InvariantCulture);
string tmpdate = "27-Apr 14:53";
DateTime TheDate = DateTime.ParseExact(tmpdate, "dd-MMM HH:mm", System.Globalization.CultureInfo.InvariantCulture);