Remove time zone offset from date in C#.Net - c#

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;

Related

Timestamp value returned with no documentation to describe what the value represents

Does anyone recognize what the value 1607293133099 might represent as a "timestamp"?
The return data type is defined as:
unsigned long long timestamp;
.. with no reference point or documentation to tell me what the value represents.
The value represents an action completed "now" result; but I am unable to convert it successfully to a datetime that is today's date and time.
Milliseconds since the start of the Unix epoch (1970): Sunday, December 6, 2020, 22:18:53.099 UTC. Is it a timestamp from about 20 minutes before you asked?
That represents this timestamp:
$ t 1607293133099
Sat Feb 3 19:44:59 CET 52903 <= Europe/Madrid
Sat Feb 3 20:44:59 EET 52903 <= Europe/Helsinki
Sat Feb 3 18:44:59 UTC 52903 <= GMT
well, year 52903 is still a little far, but... if you truncate the last three digits, it becomes:
$ t 1607293133
Sun Dec 6 23:18:53 CET 2020 <= Europe/Madrid
Mon Dec 7 00:18:53 EET 2020 <= Europe/Helsinki
Sun Dec 6 22:18:53 UTC 2020 <= GMT
which is far more suitable. The number(the original you posted) represents the number of milliseconds transcurred between the date you are timestamping and the epoch of 1/1/1970 00:00:00 UTC

Add months on date but gives me 12:00 AM in c#

DateTime dtshow = DateTime.Now.Date.AddMonths(3);
Deadline.Text = dtshow.ToLongDateString().ToString();
Jun 15, 2016 at 01:12 AM
Sep 15, 2016 at 12:00 AM
If I add 3 months in "June 15,2016 at 01:12AM" it must give me a result of "Sep 15, 2016 at 1:12 AM". I wonder why the result gives me 12:00AM.
Please help. thanks!
You're only selecting the date component:
DateTime.Now.Date.AddMonths(3)
^--- here
If you want both the date and the time component, use the original value instead of filtering it:
DateTime.Now.AddMonths(3)
The .Date call returns only the date portion, stripping the time. Try:
DateTime dtshow = DateTime.Now.AddMonths(3);

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'");

How can I convert exited sql date?

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.

The string was not recognized as a valid DateTime. There is an unknown word starting at index 26

I am working on XML feed from RSS, i thinking of sorting the feed by their published date and time. Since different RSS feeds are being taken, they have different data and time formats.
for example
Sat, 23 Mar 2013 23:19:54 GMT
Sat, 23 Mar 2013 23:19:54 EDT
Sat, 23 Mar 2013 23:19:54 -0400
I want to convert them in local time and then sort them.
var rssFeed = from el in doc.Elements("rss").Elements("channel").Elements("item")
select new
{
Title = el.Element("title").Value,
Link = el.Element("link").Value,
Description = el.Element("description").Value,
PubDate = DateTime.Parse(el.Element("pubDate").Value, null,
DateTimeStyles.None)
};
Please let me know how to proceed.
may be your DateTime string is not in correct format to be parsed.
use this instead:
DateTime.ParseExact(el.Element("pubDate").Value,
"ddd, dd MM yyyy HH:mm:ss",null);
and pass your appropriate format to get your date.
you will have to get rid of that TimeZone portion in the end.
read more about this here and here

Categories

Resources