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.)
Related
In a C# application, I have to parse an xml file which is an export of a lotus notes database.
The exports contains dates in this format :
<noteinfo noteid='6706' unid='6B6A3ADD41061773C12580F2004E8EB3' sequence='6'>
<created>
<datetime dst='true'>20170329T161803,39+02</datetime>
</created>
<modified>
<datetime>20171108T100439,39+01</datetime>
</modified>
<revised>
<datetime>20171108T100439,38+01</datetime>
</revised>
<lastaccessed>
<datetime>20171108T100439,38+01</datetime>
</lastaccessed>
<addedtofile>
<datetime dst='true'>20170329T163711,21+02</datetime>
</addedtofile>
</noteinfo>
I have to extract these dates into a .Net Datetime value.
However, I don't get what's this format. Trying to parse the date fails:
var created = DateTime.Parse("20170329T161803,39+02");
Throws
String was not recognized as a valid DateTime
How to parse this date format ?
The first part of the date, before the comma, is obvious. The second part is less. +02 probably match the GMT+02 timezone, but I don't get the 39
PS: I don't have control on the exports
Assuming ,39 are the milliseconds
var created = DateTime.ParseExact("20170329T161803,38+02", "yyyyMMddTHHmmss,ffz", CultureInfo.InvariantCulture);
Reference: DateTime.ParseExact
Try
var created = DateTime.ParseExact("20170329T161803,39+02".Remove(15), "yyyyMMdd'T'HHmmss",CultureInfo.InvariantCulture);
I'm not sure what the ,39+02 is in terms of a time - zone perhaps? I trimmed it off, but if you can describe what it is maybe it can be parsed
Edit:
Assuming that's milliseconds and timezone:
var created = DateTime.ParseExact("20170329T161803,39+02", "yyyyMMdd'T'HHmmss','ffz",CultureInfo.InvariantCulture);
I have a windows service application in which I am getting the current date and time using DateTime.Now.ToString(), which returns '04-05-2018 05:50:12'.
But I tried the same in a sample console application, but it returns the date in a different format as '5/4/2018 5:51:32 AM'
Both these machines are being executed in the same machine. Can some one let me know why is there a date format difference in these applications?
The DateTime.ToString() formats the DateTime according to current culture. As Written in the Documentation
Converts the value of the current DateTime object to its equivalent
string representation using the formatting conventions of the current
culture.(Overrides ValueType.ToString().)
If you want the same string you should instead use the DateTime.ToString(string) overload and provide the exact format which you want.
The ToString(String) method returns the string representation of a
date and time value in a specific format that uses the formatting
conventions of the current culture; for more information, see
CultureInfo.CurrentCulture.
The format parameter should contain either a single format specifier
character (see Standard Date and Time Format Strings) or a custom
format pattern (see Custom Date and Time Format Strings) that defines
the format of the returned string. If format is null or an empty
string, the general format specifier, 'G', is used.
Some uses of this method include:
Getting a string that displays the date and time in the current
culture’s short date and time format. To do this, you use the “G”
format specifier.
Getting a string that contains only the month and year. To do this,
you use the “MM/yyyy” format string. The format string uses the
current culture’s date separator.
Getting a string that contains the date and time in a specific format.
For example, the “MM/dd/yyyyHH:mm” format string displays the date and
time string in a fixed format such as “19//03//2013 18:06". The format
string uses “/” as a fixed date separator regardless of
culture-specific settings.
Getting a date in a condensed format that could be used for
serializing a date string. For example, the "yyyyMMdd" format string
displays a four-digit year followed by a two-digit month and a
two-digit day with no date separator.
I've created a webtest and have a CSV data source that contains a column with a list of short dates (MM/dd/yyyy)
I need to manipulate the parameter due to part of the web page I'm testing has a form parameter that needs it to be formatted as yyyyMMdd
When the date that is captured from the data source (ex: 02/12/2016), I noticed in the Context tab of my test run that the format to "2/12/2016 12:00:00 AM"
I've created a Request plug-in and added the following code:
public override void PreRequest(object sender, PreRequestEventArgs e)
{
base.PreRequest(sender e)
string CSVDate = e.WebTest.Context["<datasource date column>"].ToString();
DateTime dt = DateTime.ParseExact(CSVDate, "MM/dd/yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);
e.WebTest.Context.Add("NewDate", dt.ToString("yyyyMMdd"));
}
This generates a String was not recognized as a valid DateTime error. I tried changing the format to MM/dd/yyyy, but I encountered the same error.
Does anyone know how the correct DateTime format I should be using?
The date-time as shown in the context is 2/12/2016 12:00:00 AM. This has only one digit for the month whereas the format specifier has MM which wants two digits. The date-time also contains the letters AM that are not matched by the format.
Modifying the format to be M/dd/yyyy HH:mm:ss matches the date-time 2/12/2016 12:00:00, but does not match the AM part. In theory the tt format specifier should match this, but it did not work for me.
Rather than using ParseExact you can use Parse and it works out the correct format. Using the following worked on the date-time string provided:
DateTime dt1 = DateTime.Parse(CSVDate, new System.Globalization.CultureInfo("en-US"));
The CultureInfo is needed because the input date has the month and the days the wrong way around.
However, the real problem is in the way CSV files are handled by the web test. It appears to read them using the same logic as Microsoft Excel uses when reading CSVs. Namely, if it looks like a date then convert it to a date. So any string matching dd/dd/dddd (where d is a digit) might be connverted to a date. (E.g. 15/15/2017 will not be converted because 15 is not a month number.) I recommend rewriting the CSV to format the input date differently, use something that Excel would not treat as a date. One option is to have the date in three columns of the CSV, so have explicit day,monthandyearcolumns. Another option is to add non-date characters to the string and format it correctly, eg asz20160212and then remove thezwithin the web test. Generally, I would advise to avoid the conversion of string toDateTime` then another conversion to a different string.
I want to convert my JSON formatted date to C# DateTime variable. Trying to convert it with Convert.ToDateTime
("2016-01-15T11:44:52-07:00")
is giving me this output
"1/16/2016 12:14:52 AM"
I am not able to find out whether it is a correct output or not because my input date is 15 Jan 2016 but in output it is 16 Jan 2016.
How can I convert a JSON date value to a C# date value?
Looks like your current timezone is UTC +05:30 right now and that's why Convert.ToDateTime method adds those value to the result and generates 1/16/2016 00:14:52 as a value.
Since your string has an offset part, I would parse it to DateTimeOffset instead of Datetime.
var dto = DateTimeOffset.Parse("2016-01-15T11:44:52-07:00");
This will generate {15.01.2016 11:44:52 -07:00} as a DateTimeOffset.
But since you said this is related with Json, this technology should have some methods to parse it as well. It would be better to use those methods but I'm not familiar with JSON.
You seem to have problems with the time zones. Try parsing with DateTimeStyles.RoundtripKind
using System.Globalization;
var s1 = "2016-01-15T11:44:52-07:00";
var date = DateTime.Parse(s1, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind);
Probably a simple question -
I'm reading in data from a number of files.
My problem is, that when I'm reading in the date from an american file, I parse it like so:
DateSold = DateTime.Parse(t.Date)
This parses the string t.Date into a date format, however it formats the american date to a european date, e.g.
If the date is in the file as 03/01/2011, it is read as the 3rd of January, 2011, when it should be the 1st of March 2011.
Is there a way of doing this so that it formats to the european date?
var dt = DateTime.ParseExact(t.Date, "MM/dd/yyyy", CultureInfo.InvariantCulture);
The DateTime itself has no formatting, it is only when you convert it to or from a string that the format is relevant.
To view your date with American format, you pass the format to the ToString method
string americanFormat = dt.ToString("MM/dd/yyyy");
If you are parsing the date from a file which is specifically a US formatted file then simply pass the US culture information into the parse function as follows;
var usCulture = "en-US";
var dateValue = DateTime.Parse(dateString, new CultureInfo(usCulture, false));
This way you can simply swap out the culture string per different region required for parsing. Also, you no longer have to research the specific datetime format nuances for each culture as .Net will take care of this for you as designed.
Use DateTime.ParseExact or DateTime.TryParseExact when parsing, and specify a format string when you format with ToString too.
Note that there's no such thing as "an American date" after it's been parsed. The DateTime value has no concept of formatting.
It sounds like you're not actually interested in the Parse part so much as the formatting part, e.g.
string formatted = dt.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);
... but I would recommend that you control both the parsing and formatting explicitly.
If you have different file formats, you'll need to give different format strings when you read each file. How you then format the data is a separate decision.
If you know the format ahead of time, you can use DateTime.ParseExact, using the American format as your format string.
string formatteddate=DateTime.Now.ToString("d") // output: 11/8/2012
string formatteddate=DateTime.Now.ToString("D") // output: Monday, November 08, 2012
string formatteddate=DateTime.Now.ToString("f") // output: Monday, November 08, 2012 3:39 PM
string formatteddate=DateTime.Now.ToString("g") // output: Monday, November 08, 2012 3:39:46 PM
string formatteddate=DateTime.Now.ToString("d") // output: 11/8/2012 3:39 PM
More date-time format in asp.net is given here.
http://dateformat.blogspot.in/2012/09/date-time-format-in-c-aspnet.html