I have following string stored in datatable's column with varchar or string datatype
"4/29/2013 9:00:00 PM"
Now i want to convert this string to datetime
Or
let me make more clear
I just want to extract time part from this string "4/29/2013 9:00:00 PM"
and store it in Database.
Please help me.
Thanks in advance
// String to DateTime
String MyString;
MyString = "4/29/2013 9:00:00 PM";
DateTime MyDateTime;
MyDateTime = new DateTime();
MyDateTime = DateTime.ParseExact(MyString, "M/dd/yyyy hh:mm:ss tt",
null);
this should work for you.
Ok so this is a fairly simple operation you can use DateTime date = DateTime.Parse(dbString) and then use date.ToString("hh:mm tt") To get just the time and AM/PM. in the form "09:00 PM"
'hh' stands for the hours 01 to 12,
'mm' stands for the minutes 00 to 59,
'tt' stands for AM/PM
You could also use date.ToString("h:mm tt") to get the form "9:00 PM".
DateTime yourDateTime = DateTime.ParseExact(dateString, "MM/dd/yyyy HH:mm:ss tt", CultureInfo.InvariantCulture);
Try this :
string myDateTimeString = "4/29/2013 9:00:00 PM";
DateTime myDateTime = DateTime.Parse(myDateTimeString);
string myTimeString = myDateTime.Date.ToString("hh:mm tt");
Hope this helps!
Time part of string in SQL :
select DATEPART(HOUR , cast('4/29/2013 9:00:00 PM' as datetime) ) as hr
, DATEPART(MINUTE , cast('4/29/2013 9:00:00 PM' as datetime) ) as minute
, DATEPART(SECOND , cast('4/29/2013 9:00:00 PM' as datetime) ) as sec
Or try this ext:
public static class Ext
{
public static string AsLocaleDateString(this DateTime dt)
{
var format = Thread.CurrentThread.CurrentUICulture.DateTimeFormat;
return dt.ToString(format.ShortDatePattern);
}
public static DateTime? AsLocaleDate(this string str)
{
if (str.IsNullEmpty())
{
return null;
}
var format = Thread.CurrentThread.CurrentUICulture.DateTimeFormat;
return DateTime.ParseExact(str, format.ShortDatePattern, CultureInfo.InvariantCulture, DateTimeStyles.None);
}
}
Related
I have a string containing value in the format of 11/22/2019 00:00:00
I want to store this in a DateTime column.
I have tried this:
DateTime date ;
DateTime dtVal;
// string abcd contains 11/22/2019 00:00:00
abcd = date.ToString("MM/dd/yyyy hh:mm:ss", CultureInfo.InvariantCulture);
if (DateTime.TryParseExact(abcd, "MM/dd/yyyy hh:mm:ss", System.Globalization.CultureInfo.InvariantCulture, DateTimeStyles.None, out converteddate))
{
dtVal = converteddate; // I am getting 22/11/2019 12:00:00
}
I want the dtval value to hold the datetime value as - 11/22/2019 00:00:00
How to achieve this ?
Thanks
DateTime itself does not have its "natural" format, as John commented - it is internally "just a number" ;-) . The ToString() method depends on Culture you set.
I read date-time strings from a file in 2 different formats:
19/02/2019 08:24:59
2/17/2019 12:25:46 PM
For the first format the custom format string I wrote is:
string firstDate = "19/02/2019 08:24:59";
string customFormatForFirstDateTimeString = "dd/mm/yyyy hh:mm:ss";
and I use it as follows:
string firstResultingDateAndTime;
bool parsingSuccessful = DateTime.TryParseExact(
firstDate,
customFormatForFirstDateTimeString,
System.Globalization.CultureInfo.InvariantCulture,
System.Globalization.DateTimeStyles.None,
out firstResultingDateAndTime);
The problem is that parsingSuccessful results false.
For the second date-time string, the code is as follows:
string secondDate = "2/17/2019 12:25:46 PM";
string customFormatForSecondDateTimeString = "m/dd/yyy hh:mm:ss PM";
string secondResultingDateAndTime;
parsingSuccessful = DateTime.TryParseExact(
secondDate,
customFormatForSecondDateTimeString,
System.Globalization.CultureInfo.InvariantCulture,
System.Globalization.DateTimeStyles.None,
out secondResultingDateAndTime);
Also here I receive
parsingSuccessful == false;
I reckon that the custom format strings do not fit the date-time strings, but I was not able to figure out why.
Please help.
Thank you in advance.
Well, mm stands for minutes, not months (we have MM for it) that's why dd/mm/yyyy format should be dd/MM/yyyy.
Another issue with hour format where we have hh for 0..12 range (with tt for AM/PM) and HH for 0..23 interval:
string firstDate = "19/02/2019 08:24:59";
// Since we don't have AM / PM we can conclude that hour is in 0..23 range
string customFormatForFirstDateTimeString = "dd/MM/yyyy HH:mm:ss";
string secondDate = "2/17/2019 12:25:46 PM";
string customFormatForSecondDateTimeString = "M/dd/yyyy hh:mm:ss tt";
I am trying to convert my date string to MonthDate format
Date string is in this format "08-07-2016 00:00:00"
I need this output: July 8
I am trying String.Format("{0:ddd, MMM d, yyyy}", dt) but it is not working.
replace
String.Format("{0:ddd, MMM d, yyyy}", dt)
with
String.Format("{0:MMMM d}", dt)
MMMM is the name of the month
d is the day without leading 0
Reference: https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx
Try this:
string.Format("{0:MMMM d}", dt);
In C# 6.0 you can do it like this:
$"{dt:MMMM d}";
With "MMM" you get the short version of each month. With "MMMM" you get the full name.
Have you tried the following?:
date.ToString("MMMM d")
Example:
DateTime date = DateTime.Parse("08-07-2016 00:00:00");
Console.WriteLine(date.ToString("MMMM d"));
Results to:
July 8
If your date is a string, try this :
string myDate = "08-07-2016 00:00:00";
DateTime myDateValue = DateTime.ParseExact(myDate, "dd-MM-yyyy HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.None);
string myDateString = string.Format("{0: MMMM d}", myDateValue);
I am getting the account expiry date of the employee from database in a string variable and the values which is in this string variable is in the format : 6/26/2016 9:14:03 AM. Now i want this format to be converted in dd-mmm-yyyy hh:mm:ss am/pm.How can i resolve this.
I tried the following code :
DateTime passexpiredate = DateTime.ParseExact(app_user.GetPasswordExpiry(empCode), "dd-MMM-yyy hh:mm tt", null);
lblPassExpiry.InnerText = "Password Expiry Date: "+passexpiredate.ToString();
Life is to easy using DateTime.ParseExact:
DateTime.ParseExact("12/02/21 10:56:09 AM", "yy/MM/dd HH:mm:ss tt", CultureInfo.InvariantCulture).ToString("dd-MMM-yyyy HH:mm:ss tt");
You've to pass the required date time format in ToString() method as mentioned below:
DateTime passexpiredate = DateTime.Parse(app_user.GetPasswordExpiry(empCode));
lblPassExpiry.InnerText = "Password Expiry Date: " + passexpiredate.ToString("dd-MMM-yyy hh:mm tt");
You need to make DateTime from string 6/26/2016 9:14:03 AM. And then format it to dd-mmm-yyyy hh:mm:ss am/pm
DateTime.ParseExact requires you to specify format exactly as it matches with string representation of datetime value. So that's not going to work for your case.
You will have to first convert string to DateTime instance and then format it while displaying.
string date = "6/26/2016 9:14:03 AM";
var dt = DateTime.Parse(date);
var dtStr = dt.ToString("dd-mmm-yyyy hh:mm:ss tt");
Console.WriteLine(dtStr); // Output: 26-14-2016 09:14:03 AM
string DateCreated = "2012-05-24 12:34:40.060"; USA culture
How do I create the string into a Date time object with a format that include the month,
day, year, hour, minutes, seconds, milliseconds.
I think the format is this "MM/dd/yyyy hh:mm:ss.fff tt" but not sure.
** ** = underline in red, its an error
I need the datetime object because I want to pass it through another function that only accept a value of "DateTime" and not a string
Any help would be appreciated.
Thank You
this is what I have so far but i know its wrong
string DateCreated = "2012-05-24 12:34:40.060";
DateTime dt = **new DateTime(DateCreated);**
DateTime dateCreated = **dateCreated.ToString("MM/dd/yyyy hh:mm:ss.fff tt")**;
var dateTime = DateTime.ParseExact(DateCreated, "yyyy-MM-dd HH:mm:ss.fff");
string dateCreated = "2012-05-24 12:34:40.060";
DateTime myDate = DateTime.ParseExact(dateCreated, "yyyy-MM-dd HH:mm:ss.fff",
System.Globalization.CultureInfo.InvariantCulture)
you can also use
Convert.ToDateTime Method (String)