I'm writing some dates to sql from a text file. In text file date format like 10.01.2013(dd.mm.yyyy) but when i write it on sql it seems `2013-01-10 what should to add it as 10.01.2013
Here is my code:
en.LogDateTime = DateTime.Parse(myDateString);
you can use the DateTime.ParseExact method to specify exactly what format your input string should be
DateTime dt = DateTime.ParseExact("10.01.2013", "MM.dd.yyyy", CultureInfo.InvariantCulture);
DateTimeFormatInfo dateformat= new DateTimeFormatInfo();
dateformat.ShortDatePattern = "dd.MM.yyyy";
dateformat.DateSeparator = ".";
DateTime objDate = Convert.ToDateTime(strDate, dateformat);
if you want to specify the format while inserting the data into table you can use following sql query:
dateformat dmy;
insert into mytable values(mydatetime);
Related
I am trying to format a date. I get the date from the database in the dateTime format and i want to show only the date part not the time.
string date = dt.Rows[0]["declaration_date"].ToString();
string dateFormat = date.ToString("MM/DD/YYYY");
This is not working. Any help?
For date.ToString with formatting to work, date needs to be a DateTime. Try changing your code to something like this:
DateTime date = (DateTime)dt.Rows[0]["declaration_date"];
string dateFormat = date.ToString("MM/dd/yyyy");
Also, your formatting string should be MM/dd/yyyy.
You are doing it almost correctly.
Try this:
string date = dt.Rows[0]["declaration_date"].ToString("MM/dd/yyyy");
The point is that you have to apply .ToString("MM/dd/yy") to a DateTime object, not to a string.
I am not able to convert the c# date time7/31/2017 3:13:49 PM to SQL date time while inserting the records to the database.
I am doing this using
DateTime dt = DateTime.Parse("11/23/2010");
string toSqlDate= dt.ToString("yyyy-MM-dd HH:mm:ss");
DateTime finalDate = DateTime.Parse(toSqlDate);
But it's giving me the error.
String was not recognized as a valid DateTime.
The .Net DateTime maps directly to SQL Server DateTime. This means you don't need to worry about the display format at all, since DateTime does not have a display format.
All you need to do is pass the instance of the DateTime struct as a parameter to the SqlCommand:
SqlCommand.Parameters.Add("#DateTimeParam", SqlDbType.DateTime).Value = dt;
Bonus reading: How do I create a parameterized SQL query? Why Should I?
After changing it to year/month/date this can help!
var sqlDate = finalDate.Date.ToString("yyyy-MM-dd HH:mm:ss");
Your dateformat says 'year-month-day' while your date is 'month/day/year', that can't be right. Either change your dateformat or your date's formatting.
This should work:
DateTime dt = DateTime.Parse("2010-11-23 00:00:00");
string toSqlDate= dt.ToString("yyyy-MM-dd HH:mm:ss");
Change query to
insert into table_name (column1) values ('" + dt.Value.ToString("yyyy-MM-dd") + "')
Try the method DateTime.ParseExact and specify The date format that is suitable for you, here is an example from MSDN (https://msdn.microsoft.com/fr-fr/library/w2sa9yss(v=vs.110).aspx) :
var dateString = "06/15/2008";
var format = "d";
try {
var result = DateTime.ParseExact(dateString, format,
CultureInfo.InvariantCulture);
Console.WriteLine("{0} converts to {1}.", dateString,
result.ToString());
}
catch (FormatException)
{
Console.WriteLine("{0} is not in the correct format.", dateString);
}
when i run the below code,
string dt = "2017-07-09T17:50:21.000-0500";
DateTime date = Convert.ToDateTime(dt);
it gives me output as
7/10/2017 4:20:21 AM
where as i want my output to be
2017-07-09 17:50
update
the code #alexander-petrov gave worked
string dt = "2017-07-09T17:50:21.000-0500";
string date = DateTimeOffset.Parse(dt).DateTime.ToString("yyyy-MM-dd HH:mm");
gives output
2017-07-09 17:50
but on inserting the same to database it is adding +5 hrs to the time and inserting as
2017-07-09 22:50
This is a Round-Trip format of a DateTime specified with a DateTimeKind.Local kind.
You need to decide if your program needs to be aware of time zones or not.
You could try parsing it while supplying the System.Globalization.DateTimeStyles.RoundtripKind or System.Globalization.DateTimeStyles.AdjustToUniversal parameter to the Parse method.
If you want take offset into account then use DateTimeOffset type.
string dt = "2017-07-09T17:50:21.000-0500";
DateTimeOffset date = DateTimeOffset.Parse(dt);
// format on my machine
// 09.07.2017 17:50:21 - 05:00
Console.WriteLine(date);
// without offset
// 09.07.2017 17:50:21
Console.WriteLine(date.DateTime);
I couldn't get your date to work, as I think there is a colon missing in the last part. Adding that colon back allows me to convert the XSD date time into a SQL DATETIME using this script:
DECLARE #stringDate VARCHAR(30);
SELECT #stringDate = '2017-07-09T17:50:21.000-05:00';
DECLARE #xmlDate XML;
SELECT #xmlDate = CAST('' AS XML);
SELECT #xmlDate.value('xs:dateTime(sql:variable("#stringDate"))', 'datetime');
Results:
2017-07-09 22:50:21.000
Try:
string date = "2017-07-09T17:50:21.000-0500";
DateTime d = DateTime.ParseExact(date, "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffzzzz", null);
I have a database connected to c# windows forms application. I want to get a current date and time from database, and then to use that date and time to insert some other data into database.
When I execute a query select now() from WAMP's mySqlConsole I get what I expect: 12-04-17 12:06:28, but when I run that same query from c# and store the value to string like this: String datetime = cmdDatetime.ExecuteScalar().ToString(); the month becomes Apr in which format i cant insert into db.
Is there a way for c# to store mysql datetime in the same format as it is in mysql?
You can use the DateTime object if possible, or else you will need to convert the date time received to the format you want:
DateTime dateTime = (DateTime)cmdDatetime.ExecuteScalar();
string dateTimeString = dateTime.ToString(CultureInfo.InvariantCulture);
Using the InvariantCulture, you will always get the results in the same way, namely in the en-US settings.
Additionally, you can force the specific format too:
string dateTimeString = dateTime.ToString("dd-MM-yyyy HH:mm:ss");
You have to specify the format for example:
DateTime datet = (DateTime)cmdDatetime.ExecuteScalar();
String datetime = datet.ExecuteScalar().ToString( "dd-mm-yyy HH:mm:ss");
Try That:
DateTime datetime = (DateTime)cmdDatetime.ExecuteScalar();
String datetimeStr = datetime.ToString("yyyyMMddHHmmss");
I have a date string with dd/mm format like 06/03.Now i have to store this string into mysql table column with DATETIME format.
I am getting the problem as How can i add the current year generically because i don't want to hard code it.Subsequently how will i convert it into MySql DATETIME format for saving it.
Please help me .
You can use Parse method of DateTime:
DateTime dateTime = DateTime.Parse("06/03");
UPDATE
For your comment:
Also after parsing into DateTime i am getting date correct but time i
dont want to be 12:00:00 AM instead i want it to be 00:00:00.
12:00:00 AM corresponds to 00:00:00 only. You can verify that by getting Hour property which will return 0 and also TimeOfDay will too return 00:00:00.
Even if you try to parse exact date, it also creates the same format.
DateTime dateTime = DateTime.ParseExact("06/03 00:00:00", "dd/MM hh:mm:ss",
CultureInfo.InvariantCulture);
And you don't need conversion from DateTime object to SQL compliant DateTime object. You can pass the .Net object to SQL writer.
Consider the code:
C#
string s = "06/03";
System.DateTime dateNow = Convert.ToDateTime(s);
will give the output as you required
in VB.Net :
Dim s As String = "06/03"
Dim dateNow As Date = CDate(s)
MsgBox(dateNow)
You could do something like
var some_date = "06/03";
var year = DateTime.Now.Year;
var option = some_date+"/"+year;
Or use any of the string formats to bend it to your needs
More on date string format can be found on this MSDN page.
Edit:
If you want zeroes in the time, like your comment said, you can usit Rohit vats answer and do:
DateTime dateTime = DateTime.Parse("06/03");
var s1 = dateTime.ToString("MM/dd/yy 00:00:00");
// Output: 03/06/14 00:00:00
var s2 = dateTime.ToString("MM/dd/yyyy 00:00:00");
// Output: 03/06/2014 00:00:00