RSS publish dates - should that be in English? - c#

I create RSS items. I've a column called date_published, which is DateTime column in MS SQL database. e.g. it contains "2011-05-04 15:19:05.630"
Below is the C# code date I re-generate RSS acceptable date format (input database field).
Input:
Convert.ToDateTime(item.date_published).ToString("r")
Output:
Wed, 04 May 2011 15:19:05 GMT
So I looked with Notepad in the XML file and the result look above.
Problem:
When the users receive this item in Outlook RSS feedreader, it displays +2 hours ahead. So it displays in email outlook received date 2011-05-04 17:19:05
So I change my code from:
item.pubdate_published.Value.ToString("r")
To:
item.pubdate_published.Value.ToString("ddd dd MMM yyyy HH:mm:ss +2 G'M'T")
This gives me exact what I want like this : mer, 04 mai 2011 15:19:05 +2 GMT.
PROBLEM:
My RSS XMl file shows like that too (above), but my OUTLOOK email received date is 06/January/2011 09:32
So the only issue I can think is the language problem... Because the dates are generated in Dutch or French.
if this is the issue: How do I write the dates in English?

I found the problem... the datetime in RSS XML file must be in English. So we were generating the date sometimes Dutch and sometimes in French.
this is what I did:
item.pubdate_published.Value.ToString("r").replace("GMT",("+2 GMT");
the hours and the dates are correct now.
be careful doing like this : ToString("ddd dd MMM yyyy HH:mm:ss +2 G'M'T") will generate specific language and not always English. ("r") always generate in English.

Related

Date convert issue

I try to convert below text to date, for more than 80 cases from 2011 to 2017-July it is working fine but from Aug it converts the year to the year 2018 and considers the number as a day. I want to check is there any way to convert format date exactly and properly? this piece of code is for data migration so if it work wrongly it is disaster situation.
Convert.ToDateTime("Sep-17")
The answer in my machine is: {9/17/2018 12:00:00 AM}
the correct answer should be: {9/1/2017 12:00:00 AM}
DateTime.ParseExact("Sep-17", "MMM-yy", CultureInfo.InvariantCulture);

Dateime Parse method adding one day to input date

I am reading a date from an xml file and parsing it to a my desired format. It i adding a day to the date and i cant seem to figure out why.
input : 2014-02-12T15:21:19-08:00
output : 13 Feb 2014 01:21
Here is my code to parse date:
string date = DateTime.Parse(row["CountDate"].ToString()).ToString("dd MMM yyyy HH:mm");
Any help will be greatly appreciated.
The reason is that the timezone information is being used to adjust the time to your local time zone.
If you remove the "-08:00" suffix, you'll find that the time won't be adjusted. However, you need to know whether the timezone information is important before ignoring it!
Well it looks like what you have is a UTC date/time with an 8 hour offset, when you parse the date what you have is an instance of the local time (Parse will take into account the offset).
If you are only interested in the UTC date/time then you can parse just that particular information
DateTime.ParseExact("yyyy-MM-ddTHH:mm:ss", row["CountDate"],
CultureInfo.InvariantCulture);

Display date in arabic in this format 'Wednesday, May 22, 2013'

I have a multilingual website and I need to show the date in this format Wednesday, May 22, 2013. I use following line of code to display dates
DateTime.UtcNow.ToString("D");
That doesn't work when I use this along with Culture info
It then shows me dates in this format مايو 2013,22
How can I show date in arabic in format year, date month day
I am not sure why DateTime.UtcNow.ToString("D"); is not able to convert to arabic date with weekdays
UPDATE : this works DateTime.Now.ToString("dd dddd , MMMM, yyyy", new CultureInfo("ar-AE"))
You can specify the format explicitly:
DateTime.UtcNow.ToString("dddd, MMMM dd, yyyy");
This will output, for example:
Wednesday, May 22, 2013
A lowercase 'd' stands for the day, two 'd's for the day with a zero in front if applicable, three 'd's for the abbreviation of the name of the day and four 'd's for the complete name of the day. The same applies to the months with a capital 'M' (lowercase 'm' are minutes!).
Use CultureInfo class to convert date formats in different languages.
using System.Globalization;
DateTime.UtcNow.ToString("D", new CultureInfo("ar-AE"));

Format 2 strings to RSS PubDate

ive got 2 strings, date:"27.03.11 " and time:"15:04", which id like to format as a PubDate elemnt for a rss file like Fri, 18 Nov 2005 19:12:30 GMT.
How can i do this in c sharp?
Use the following steps:
Parse the date and time strings into one DateTime variable. Use the DateTime.ParseExact static method for this.
Convert the datetime to GMT using the methods of the TimeZone class (if desired---I don't think this is mandatory according to the RSS specification).
Format this variable into a string using the DateTime.ToString method. The following MSDN pages will help you choose the correct format string based on your needs:
Standard Date and Time Format Strings
Custom Date and Time Format Strings
Since RSS requires dates to be in the RFC 822 format, the following related SO question might help you with the last step:
How do I parse and convert DateTime’s to the RFC 822 date-time format?
EDIT: For the first step, have a look at this example:
var s = "27.03.11 15:04";
var dtm = DateTime.ParseExact(s, #"dd.MM.yy HH\:mm", null);
(The \: ensures that : is seen as a literal : rather than a culture-specific time separator.)

Getting exception when using DateTime.Parse method

So, i have this string "Date: Mon Jan 03 2011 19:29:44 GMT+0200", and when i use DateTime.Parse(date).ToString(); i'm getting "String was not recognized as a valid DateTime."
If i remove the '+0200' part it works ok, but ofcourse it doesn't show the correct local time.
What's wrong with that?
From the documentation, it seems that DateTime.Parse() only understands:
The GMT designator, used alone, e.g. Mon, Jan 03 2011 17:29:44 GMT, or
A time zone offset specified without the GMT designator, e.g. Mon, Jan 03 2011 19:29:44+02:00.
You might want to convert your date string to the second form.
It just means that the time zone offset isn't an expected part of the default format strings.
If you know what format you're expecting, I suggest you call DateTime.ParseExact (or DateTime.TryParseExact), specifying the format(s) to try. Look at the documentation for custom date/time format strings for more details.
You have two mistakes.
First - don`t use Parse method. More correct is TryParse.
Second - you will have globalisation issues, when you use Parse or TryParse without arguments.
For example, see this code:
DateTime.Parse( "01.02.2011" ); In the USA it is 2nd of January. In the Germany it is 1st of February.
So, I recomment you to use formats from this article.

Categories

Resources