473,411 Members | 2,009 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

Date format pattern for "03rd of April 2006" type date

Hi,

I'm trying to convert a string to date by specifying the format. The value
is like this: "03rd of April 2006". Now this is converted fine by using the
following format: "dd\r\d \o\f MMMM yyyy". The problem is of course that the
string can be 01st of..., 04th of etc., meaning that I can't use the "\r\d"
format description for all possible values.

Any suggestions? Is there a wildcard i can use, like "dd\?\?"...

Thanks,
Zsolt
Mar 9 '06 #1
11 2751
If you find no better way, try something like this:

switch (theDateTime.Day)
{
case 1:
// use "st" here
case 2:
// use "nd" here
case 3:
// use "rd" here
default:
// use "th" here
}

Let's write a private helper for this.

Hope it helps,
Thi

Mar 9 '06 #2
Hi Thi,

Thanks for the suggestion but I would really like not to do that :).

format = new System.Globalization.CultureInfo(CultureInfo.Invar iantCulture.LCID, false);

System.DateTime DateValue = System.DateTime.ParseExact(inputString, formatDescription, format);

Now the inputString is "03rd of April 2006" and the user have to tell me/system what the formatDescription is. It can't give me the "rd" hardcoded format, so question is if the format can be specified to cover the "st", "nd" etc cases, or we have to have something that you suggest and have to parse the format and the inputString and figure out the final format.

Regards,

Zsolt

"Truong Hong Thi" <th*****@gmail.com> wrote in message news:11*********************@e56g2000cwe.googlegro ups.com...
If you find no better way, try something like this:

switch (theDateTime.Day)
{
case 1:
// use "st" here
case 2:
// use "nd" here
case 3:
// use "rd" here
default:
// use "th" here
}

Let's write a private helper for this.

Hope it helps,
Thi

Mar 9 '06 #3

zsolt wrote:
Hi,

I'm trying to convert a string to date by specifying the format. The value
is like this: "03rd of April 2006". Now this is converted fine by using the
following format: "dd\r\d \o\f MMMM yyyy". The problem is of course that the
string can be 01st of..., 04th of etc., meaning that I can't use the "\r\d"
format description for all possible values.

Any suggestions? Is there a wildcard i can use, like "dd\?\?"...


It appears not. However, how about constructing a new string from the
first two characters of the input string, plus the ninth character
onwards, and then parsing that?

--
Larry Lard
Replies to group please

Mar 9 '06 #4
Hi Larry,
It appears not. However, how about constructing a new string from the
first two characters of the input string, plus the ninth character
onwards, and then parsing that?


Yes, that is possible and it seems that it has to be done that way. The
problem is that the input format can be anything from 2006.04.03 to the
mentioned one. Because of that, I have to find out if the format is the
problematic one and make the replace/extraction or just leave it the user's
way.

It just feels strange that it's not handled easier by the framework - it's
not that unusual format. Or maybe it is :)
Mar 9 '06 #5
I don't think there is some other way. You can write another Parse
method to parse yourself. One way is trying to remove the "rd" before
parse, something like this;

DateTime ParseDateTime(string input)
{
string s = input.SubString(0, 2) + input.SubString(4);
// now s is "03 of April 2006", you can parse it as normal
}

It is exactly what Larry suggested.

Mar 9 '06 #6
Unfortunately yes, it seems like that. Thanks

"Truong Hong Thi" <th*****@gmail.com> wrote in message
news:11**********************@e56g2000cwe.googlegr oups.com...
I don't think there is some other way. You can write another Parse
method to parse yourself. One way is trying to remove the "rd" before
parse, something like this;

DateTime ParseDateTime(string input)
{
string s = input.SubString(0, 2) + input.SubString(4);
// now s is "03 of April 2006", you can parse it as normal
}

It is exactly what Larry suggested.

Mar 9 '06 #7

zsolt wrote:
Hi Larry,
It appears not. However, how about constructing a new string from the
first two characters of the input string, plus the ninth character
onwards, and then parsing that?


Yes, that is possible and it seems that it has to be done that way. The
problem is that the input format can be anything from 2006.04.03 to the
mentioned one. Because of that, I have to find out if the format is the
problematic one and make the replace/extraction or just leave it the user's
way.

It just feels strange that it's not handled easier by the framework - it's
not that unusual format. Or maybe it is :)


I think I have written code in every language I have ever programmed
professionally, to produce the correct 'st' 'nd' 'th' suffix. The
Framework seems to be no exception, in that there doesn't seem to be a
way to produce these suffixes automatically; these are the only
datetime format patterns relating to days:

d The day of the month. Single-digit days will not have a leading zero.

dd The day of the month. Single-digit days will have a leading zero.
ddd The abbreviated name of the day of the week, as defined in
AbbreviatedDayNames.
dddd The full name of the day of the week, as defined in DayNames.

If it were up to me, there would definitely be a way of producing '1st'
etc, especially as this (like many of the format patterns) depends on
culture.

--
Larry Lard
Replies to group please

Mar 9 '06 #8
Hi,


Yes, that is possible and it seems that it has to be done that way. The
problem is that the input format can be anything from 2006.04.03 to the
mentioned one. Because of that, I have to find out if the format is the
problematic one and make the replace/extraction or just leave it the
user's way.
Can you control the format?
If not you have more than one problem at hand, first you have to see what
format you getting the date from , and sometimes it's difficult to know, a
good example 01/02/03 it can be viewed as 2nd of Jan 2003 ( in english) or
1st of feb ( in spanish)
It just feels strange that it's not handled easier by the framework - it's
not that unusual format. Or maybe it is :)


I find the particular example weird, it's the first ime I see it

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Mar 9 '06 #9
> Can you control the format?
If not you have more than one problem at hand, first you have to see what
format you getting the date from , and sometimes it's difficult to know,
a good example 01/02/03 it can be viewed as 2nd of Jan 2003 ( in english)
or 1st of feb ( in spanish)


Yes, the format is controled - the user is forced to describe the format.
The post is basically how to describe the "03rd of April 2006". The
"01/02/03" is easy when you know the format, the problem is only when you
try to guess the date without having format info. In my case I know the
format but can't describe it so the .NET understands it...
Mar 9 '06 #10
This is where C# is really lacking as a language. You should be able to
replace the Parse/ParseExact method of the DateTime class without
subclassing or requiring helper classes.

-Alan

Mar 9 '06 #11

"Larry Lard" <la*******@hotmail.com> wrote in message
news:11*********************@u72g2000cwu.googlegro ups.com...
If it were up to me, there would definitely be a way of producing '1st'
etc, especially as this (like many of the format patterns) depends on
culture.


I think that you hit the nail on hte head. It's all about cultrue, and what
if your db is in London and your client is accesing in Rome, or worse yet in
China?


Mar 15 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: G Gerard | last post by:
Hello I am having some problem comparing dates with the SQL statement below MySQL = "SELECT Format(Date, 'yyyy/mmmm/dd') as FROM TblDates WHERE _ Format(Date, 'yyyy/mmmm/dd') =...
5
by: pamelafluente | last post by:
A quick question, I have a string with a date in european format, e.g. "30/4/2006". What is the FASTEST way to produce the corresponding string in USA format "4/30/2006" and asian format...
6
by: Robert Bravery | last post by:
HI all, I'm new toJS. I am trying to get a user inputed date into mysql. The mysql database accepts the date in yyyy-mm-dd format. The user, from South Africa inputs the date in a web form in...
7
by: mr.nimz | last post by:
hello, this is antenio. recently i've come to a problem. i got a way through it, somehow, still it left me in a curious state, so i'm posting it here, if i can get an answer from some techy, ...
5
by: Henning M | last post by:
Hi all, I having some problems with Access and selecting records between dates.. When I try this in access, it works fine!! "Select * from Bilag Where Mdates Between #1/1/2006# And...
1
by: U S Contractors Offering Service A Non-profit | last post by:
Craig Somerford to New, Harvard, (bcc:Natural) More options 7:56 pm (0 minutes ago) " Working in Faith " " SNOWING over New York City Today November 7th 2006 "
1
by: U S Contractors Offering Service A Non-profit | last post by:
" Mentor-ship applied for November 8th 2006 " Craig Somerford to Harvard, Apple, Google, (bcc:Natural), (bcc:Matthew), (bcc:National), (bcc:Letters) Hide options 9:41 am (0 minutes ago) From:...
17
by: Petyr David | last post by:
Just looking for the simplest. right now my perl script returns an error messge to the user if the date string is invalid. would like to do this before accessing the server. TX
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.