How can I convert exited sql date? - c#

I am new in c#
I have a column in my fill sql database that has string format but i have two kind value data format there like this:
Jul 29 2014 12:00AM
and
11/11/2014
How can I convert all Jul 29 2014 12:00AM value to 11/11/2014 value?

No, don't do that!
A DateTime doesn't have any implicit format. It just have date and time values. There is no such a thing like;
A DateTime with Jul 29 2014 12:00AM or 11/11/2014 format.
String representations of a DateTime can have format. In such a case, Jul 29 2014 12:00AM will be just database management system representation of your DateTime
If you keep your dates in a character column, stop it! Change your column type to datetime or a related type.
If you just want to represent your DateTime with a specific format, use .ToString() like;
date.ToString("MM/dd/yyyy", CultureInfo.InvariantCulture);
And please read;
Bad habits to kick : choosing the wrong data type

Try This
String stringDate="Jul 29 2014 12:00AM";
DateTime date=DateTime.Parse(stringDate);
Console.Write(date.ToString());
Ideone
If you wanna to change in query
SELECT convert(datetime, 'Jul 29 2014 12:00AM', 101)

.ToString("MM/dd/yyyy");
use this behind an date variable and it format to month/day/year.

Related

How to convert India Standard Time to SQL date and time in C# [duplicate]

This question already has answers here:
.NET DateTime to SqlDateTime Conversion
(6 answers)
Closed 6 years ago.
I want to convert below date and time format to SQL date and time
Thu Apr 07 2016 06:30:00 GMT+0530 (India Standard Time)
any one have idea Thanks.
Since your string has UTC Offset value, I would parse it to DateTimeOffset instead of DateTime since it can hold the offset part.
But neither DateTime nor DateTimeOffset keeps time zone information, you should use GMT and (India Standard Time) parts as a string literal delimiter.
var s = "Thu Apr 07 2016 06:30:00 GMT+0530 (India Standard Time)";
var dto = DateTimeOffset.ParseExact(s, "ddd MMM dd yyyy HH:mm:ss 'GMT'zzz '(India Standard Time)'",
CultureInfo.InvariantCulture);
Now you have a DateTimeOffset as {07.04.2016 06:30:00 +05:30}.
And I would insert this dto as datetimeoffset typed column in SQL Server (with a parameterized query of course) since it saves offset part as well.
+---------------------------+
| Time zone offset range |
+---------------------------+
| -14:00 through +14:00 |
+---------------------------+
If your input is a string, you will need to start parsing the date for the specific culture:
DateTime dt = DateTime.ParseExact(inputString, System.Globalization.CultureInfo("<your_culture>"));
where <your_culture> is one of the multiple culture names for your country (see http://www.csharp-examples.net/culture-names/)
then you can get the date back as an SQL-compatible string, with simple quotation marks included:
string sqlDate = dt.ToString("'yyyy-MM-dd HH:mm:ss'");

Remove time zone offset from date in C#.Net

i am getting data in xml format which contains below fields
Sat Feb 13 2016 01:59:28 GMT+1100 (AUS Eastern Standard Time)
The value of ActivatedDate=Sat Feb 13 2016 01:59:28 GMT+1100 (AUS Eastern Standard Time)
i want to get the date and time from the ActivatedDate and compare with today's date and find the difference in time span(hour) of both the dates .
For ex: (Sat Feb 13 2016 01:59:28 - 22nd Feb 2016).Hour
I want to find the difference between two dates with respect to hours .
Please help me on this.
This is assuming that you need to parse the ActivatedDate from a string:
string activatedDate = "Sat Feb 13 2016 01:59:28";
DateTime activeDate = DateTime.Parse(activatedDate);
TimeSpan timeDifference = DateTime.Now.Subtract(activeDate);
double hoursDifference = timeDifference.TotalHours;

Bug in .NET's DateTime.ToString("R") with UTC dates?

I'm based in the UK (GMT+1 time at the moment).
If I run this:
> DateTime.UtcNow.ToString("R") // Or...
> DateTime.Now.ToUniversalTime().ToString("R")
"Mon, 06 Oct 2014 10:20:00 GMT"
Correct answer.
If I now run the same, without UTC DateTime conversion:
> DateTime.Now.ToString("R")
"Mon, 06 Oct 2014 11:20:00 GMT"
The time printed is correct, but the timezone is wrong. I would expect instead:
"Mon, 06 Oct 2014 11:20:00" // Or..
"Mon, 06 Oct 2014 11:20:00 BST"
Question: Is this behaviour by design? Can I get the same output as with the "R" format, but with the correct timezone indicator?
It's definitely not a bug, it's the documented behaviour:
The custom format string is "ddd, dd MMM yyyy HH':'mm':'ss 'GMT'". When this standard format specifier is used, the formatting or parsing operation always uses the invariant culture.
...
Although the RFC 1123 standard expresses a time as Coordinated Universal Time (UTC), the formatting operation does not modify the value of the DateTime object that is being formatted. Therefore, you must convert the DateTime value to UTC by calling the DateTime.ToUniversalTime method before you perform the formatting operation. In contrast, DateTimeOffset values perform this conversion automatically; there is no need to call the DateTimeOffset.ToUniversalTime method before the formatting operation.
As I noted in a comment on the question, 10:20 GMT is correct, assuming that you ran the code shortly before asking the question: 11:20 GMT has not occurred yet.
So basically, when you follow the guidance in the documentation and call ToUniversalTime, it does the right thing. When you don't, it gives a misleading value - that's unfortunate, but part of the broken design of DateTime IMO.
You should consider at least using DateTimeOffset, or potentially using my Noda Time project instead.

How to convert a string containing a EDT related instant to a DateTime?

I have a string in this format:
"Fri, 18 Mar 2011 18:53:15 EDT"
I want to convert this to datetime object, the problem is with EDT, at the end. If I use GMT DateTime.TryParse works fine, but if it is something else like EDT, it return false.
Check out the TimeZoneInfo class and this question, or the definitive time zone guide.

DateTime to RFC-1123 gives inaccurate timezone

If I get the RFC-1123 formatted date of a DateTime object, it gives the current local time, but gives the timezone as GMT (which is inaccurate).
DateTime.Now.ToString("r");
returns
Fri, 12 Feb 2010 16:23:03 GMT
At 4:23 in the afternoon, but my timezone is UTC+10 (plus, we're currently observing daylight saving time).
Now, I can get a return value that's "correct" by converting to UTC first:
DateTime.UtcNow.ToString("r");
returns
Fri, 12 Feb 2010 05:23:03 GMT
However, ideally, I'd like to get the right timezone, which I guess would be
Fri, 12 Feb 2010 16:23:03 +1100
Passing in the current CultureInfo doesn't change anything. I could get a UTC offset with TimeZoneInfo.Local.GetUtcOffset(...) and format a timezone string from that, but stripping out the GMT bit and replacing it seems gratutiously messy.
Is there a way to force it to include the correct timezone?
The .NET implementation always expresses the result as if it were GMT, irrespective of the time offset of the actual date.
By using DateTime.Now.ToString("r"); you're essentially saying String.Format("ddd, dd MMM yyyy HH':'mm':'ss 'GMT'", DateTime.Now);, which is the .NET RFC1123 format string, as indicated on MSDN - The RFC1123 ("R", "r") Format Specifier.
To get the behaviour you require, you should probably use String.Format, and replace the fixed 'GMT' section of the specifier with a time offset specifier:
The "z" Custom Format Specifier
The "zz" Custom Format Specifier
The "zzz" Custom Format Specifier
You could just do DateTime.UtcNow.ToString ("R"), you will still get GMT timezone but the time is correctly offset then.

Categories

Resources