473,324 Members | 1,856 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,324 software developers and data experts.

date code and string reverse

I have a need to reverse a date to show as a "date code". For example
today, 090507 would be coded as 905070 (reversing the parts of the date.

This is what i have but there has to be a better way.

DateTime d = DateTime.Now;
string datecodemonth = d.ToString("MM");
datecodemonth = datecodemonth[1] + datecodemonth[0];
string datecodeday = d.ToString("dd");
datecodeday = datecodeday[1] + datecodeday[0];
string datecodeyear = d.ToString("yy");
datecodeyear = datecodeyear[1] + datecodeyear[0];
string datecode = datecodemonth + datecodeday + datecodeyear;

Ideas? Is there anything in the format of the date that will do this?

dan
Sep 5 '07 #1
11 3959
Seems a very odd thing to want to do, but why not ;-p
If this is high volume, then to avoid lots of intermediate strings,
try to work in a char[] buffer, something like:

static void Main(string[] args) {
char[] buffer =
DateTime.Today.ToString("MMddyy").ToCharArray();
Swap(buffer, 0, 1); // perhaps unroll...
Swap(buffer, 2, 3);
Swap(buffer, 4, 5);
string s = new string(buffer);
Debug.WriteLine(s);
}
static void Swap(char[] data, int index1, int index2) {
char c = data[index1];
data[index1] = data[index2];
data[index2] = c;
}

Sep 5 '07 #2
"Dan Holmes" <da*******@bigfoot.comwrote in message
news:OU*************@TK2MSFTNGP06.phx.gbl...
Ideas? Is there anything in the format of the date that will do this?
Firstly, create a function to reverse a string:

string StringReverse(string pstrString)
{
char[] strArray = pstrString.ToCharArray();
Array.Reverse(strArray);
return new string(strArray);
}

Then do the following:

string strDateLiteral = "090507";
DateTime dtmDate = DateTime.ParseExact(strDateLiteral, "MMddyy", null);
string strDateReversed =
StringReverse(dtmDate.ToString("MM")) +
StringReverse(dtmDate.ToString("dd")) +
StringReverse(dtmDate.ToString("yy"));
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Sep 5 '07 #3

On Sep 5, 11:39 pm, Dan Holmes <danhol...@bigfoot.comwrote:
I have a need to reverse a date to show as a "date code". For example
today, 090507 would be coded as 905070 (reversing the parts of the date.
Both Mark's and Marc's examples use the default DateTime string format
provider, and then manipulates its results. A more .NET-ty solution
might be to implement a custom format provider, and use that to create
a string from a DateTime instead of the default provider. This would
avoid first going to 090507 and then to 905070, going directly to
905070 from a DateTime instance. See here for an example on how to
implement a custom format provider: http://www.codeproject.com/csharp/custstrformat.asp

Sep 5 '07 #4
Dan Holmes wrote:
I have a need to reverse a date to show as a "date code". For example
today, 090507 would be coded as 905070 (reversing the parts of the date.

This is what i have but there has to be a better way.

DateTime d = DateTime.Now;
string datecodemonth = d.ToString("MM");
datecodemonth = datecodemonth[1] + datecodemonth[0];
string datecodeday = d.ToString("dd");
datecodeday = datecodeday[1] + datecodeday[0];
string datecodeyear = d.ToString("yy");
datecodeyear = datecodeyear[1] + datecodeyear[0];
string datecode = datecodemonth + datecodeday + datecodeyear;

Ideas? Is there anything in the format of the date that will do this?
You can do this with string operations or perhaps with a custom format
provider. I would however go for a numeric method, as the source is numeric.

Something like:

DateTime d = DateTime.Now;
int year = d.Year % 100;
int month = d.Month;
int day = d.Day;
string dateCode =
(month % 10).ToString() +
(month / 10).ToString() +
(day % 10).ToString() +
(day / 10).ToString() +
(year % 10).ToString() +
(year / 10).ToString();

--
Göran Andersson
_____
http://www.guffa.com
Sep 6 '07 #5
"Göran Andersson" <gu***@guffa.comwrote in message
news:OR**************@TK2MSFTNGP06.phx.gbl...
Dan Holmes wrote:
>I have a need to reverse a date to show as a "date code". For example
today, 090507 would be coded as 905070 (reversing the parts of the date.

This is what i have but there has to be a better way.

DateTime d = DateTime.Now;
string datecodemonth = d.ToString("MM");
datecodemonth = datecodemonth[1] + datecodemonth[0];
string datecodeday = d.ToString("dd");
datecodeday = datecodeday[1] + datecodeday[0];
string datecodeyear = d.ToString("yy");
datecodeyear = datecodeyear[1] + datecodeyear[0];
string datecode = datecodemonth + datecodeday + datecodeyear;

Ideas? Is there anything in the format of the date that will do this?

You can do this with string operations or perhaps with a custom format
provider. I would however go for a numeric method, as the source is
numeric.

Something like:

DateTime d = DateTime.Now;
int year = d.Year % 100;
int month = d.Month;
int day = d.Day;
string dateCode =
(month % 10).ToString() +
(month / 10).ToString() +
(day % 10).ToString() +
(day / 10).ToString() +
(year % 10).ToString() +
(year / 10).ToString();

StringBuilder people...StringBuilder!!!

--
Doug Semler, MCPD
a.a. #705, BAAWA. EAC Guardian of the Horn of the IPU (pbuhh).
The answer is 42; DNRC o-
Gur Hfrarg unf orpbzr fb shyy bs penc gurfr qnlf, abbar rira
erpbtavmrf fvzcyr guvatf yvxr ebg13 nalzber. Fnq, vfa'g vg?

Sep 7 '07 #6
Doug Semler wrote:
"Göran Andersson" <gu***@guffa.comwrote in message
news:OR**************@TK2MSFTNGP06.phx.gbl...
>Dan Holmes wrote:
>>I have a need to reverse a date to show as a "date code". For
example today, 090507 would be coded as 905070 (reversing the parts
of the date.

This is what i have but there has to be a better way.

DateTime d = DateTime.Now;
string datecodemonth = d.ToString("MM");
datecodemonth = datecodemonth[1] + datecodemonth[0];
string datecodeday = d.ToString("dd");
datecodeday = datecodeday[1] + datecodeday[0];
string datecodeyear = d.ToString("yy");
datecodeyear = datecodeyear[1] + datecodeyear[0];
string datecode = datecodemonth + datecodeday + datecodeyear;

Ideas? Is there anything in the format of the date that will do this?

You can do this with string operations or perhaps with a custom format
provider. I would however go for a numeric method, as the source is
numeric.

Something like:

DateTime d = DateTime.Now;
int year = d.Year % 100;
int month = d.Month;
int day = d.Day;
string dateCode =
(month % 10).ToString() +
(month / 10).ToString() +
(day % 10).ToString() +
(day / 10).ToString() +
(year % 10).ToString() +
(year / 10).ToString();


StringBuilder people...StringBuilder!!!
Why? A StringBuilder doesn't perform better, and there is no scalability
issues here.

If you want performance, this is four times faster:

DateTime d = DateTime.Now;
int year = d.Year % 100;
int month = d.Month;
int day = d.Day;
char[] c = new char[6];
c[0] = (char)((month % 10) + 48);
c[1] = (char)((month / 10) + 48);
c[2] = (char)((day % 10) + 48);
c[3] = (char)((day / 10) + 48);
c[4] = (char)((year % 10) + 48);
c[5] = (char)((year / 10) + 48);
return new string(c);

--
Göran Andersson
_____
http://www.guffa.com
Sep 7 '07 #7
On Sep 7, 8:14 am, Göran Andersson <gu...@guffa.comwrote:
Doug Semler wrote:
"Göran Andersson" <gu...@guffa.comwrote in message
news:OR**************@TK2MSFTNGP06.phx.gbl...
Dan Holmes wrote:
I have a need to reverse a date to show as a "date code". For
example today, 090507 would be coded as 905070 (reversing the parts
of the date.
>This is what i have but there has to be a better way.
>DateTime d = DateTime.Now;
string datecodemonth = d.ToString("MM");
datecodemonth = datecodemonth[1] + datecodemonth[0];
string datecodeday = d.ToString("dd");
datecodeday = datecodeday[1] + datecodeday[0];
string datecodeyear = d.ToString("yy");
datecodeyear = datecodeyear[1] + datecodeyear[0];
string datecode = datecodemonth + datecodeday + datecodeyear;
>Ideas? Is there anything in the format of the date that will do this?
You can do this with string operations or perhaps with a custom format
provider. I would however go for a numeric method, as the source is
numeric.
Something like:
DateTime d = DateTime.Now;
int year = d.Year % 100;
int month = d.Month;
int day = d.Day;
string dateCode =
(month % 10).ToString() +
(month / 10).ToString() +
(day % 10).ToString() +
(day / 10).ToString() +
(year % 10).ToString() +
(year / 10).ToString();
StringBuilder people...StringBuilder!!!

Why? A StringBuilder doesn't perform better, and there is no scalability
issues here.

If you want performance, this is four times faster:

DateTime d = DateTime.Now;
int year = d.Year % 100;
int month = d.Month;
int day = d.Day;
char[] c = new char[6];
c[0] = (char)((month % 10) + 48);
c[1] = (char)((month / 10) + 48);
c[2] = (char)((day % 10) + 48);
c[3] = (char)((day / 10) + 48);
c[4] = (char)((year % 10) + 48);
c[5] = (char)((year / 10) + 48);
return new string(c);
It only comes out 2.3 times faster for me, but there we go. This
version is between the two, and I prefer it for readability:

DateTime d = DateTime.Now;
string original = d.ToString("yyddMM");
char[] c = original.ToCharArray();
Array.Reverse(c);
return new string(c);

Jon

Sep 7 '07 #8
[Smacks head] Why didn't I thnk of that!

Marc
Sep 7 '07 #9
Doug Semler <do********@gmail.comwrote:
The compiler, in cases such as the one above, can optimize alot of the
instantiations out of the picture that you wouldn't see. This is because,
even though a string is treated as an Object, at runtime it is treated as a
Value class (struct).
No, it is certainly *not* treated as a struct. It's treated as a
reference type in every possible way.
Therefore, the compiler can actually perform compile
time value optimizations on strings. A simple example:

string MyFunc(int a, int b)
{
string sa = a.ToString();
string sb = b.ToString();
return sa + sb;
}

The compiler is smart enough to convert that into a concatination that is
back filled (basically what happens is an array of strings is created with
all of the "ToString" calls, on FINAL string is created, and the characters
of each string are then filled into the final string).
Um, any evidence of that?

I believe that 3 completely separate strings will be created in the
above code. There's no special optimisation going on as far as I'm
aware.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 8 '07 #10
Um, any evidence of that?
I agree with Jon; the real demonstration here is when you have more
than 2 strings:

string d = a + b + c; // 3 existing strings

The point is, it /doesn't/ evaluate the left-to-right, which is what
you might reasonably expect, but which would create intermediate
strings (i.e. tmp = a+b, d = tmp + c). Instead the complier swaps it
for:

string d = string.Concat(a,b,c);

Internally, string.Concat finds the sum of all the strings, allocates
a buffer big enough for all 3, and inserts the provided strings into
the buffer one after the other. Generally, you would have to use a
char[] buffer for this, but since the new string hasn't seen the
outside world yet, the runtime actually uses a string directly and
edits the (otherwise immutable) string directly.

Marc

Sep 8 '07 #11
is there expert opinion (yourselves included of course!) on when
to use String and when to use StringBuilder?
Jon's page is (as usual) quite illuminating:
http://www.pobox.com/~skeet/csharp/stringbuilder.html

Basically, if you are concatenating strings in a loop, then
StringBuilder is your friend. If you are concatenating strings in a
single line (i.e. someVar + "alkfj" + someOtherVar + whatever + "
items"), then just let the compiler do the work. You can also use
StringBuilder to retain editability of the contents while
concatenating, but I've never had to use this (I prefer to simply
build the correct string as I go)).
If you are writing an über-string (perhaps file contents, CSV
maybe...) then a third approach is to use a TextWriter to write
directly to an output Stream (such as a file, or an HttpResponse).
This can be even more efficient, as the working data is minimised -
very useful for server work or volume processing.

Marc

Sep 8 '07 #12

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

Similar topics

2
by: Robin | last post by:
I know that you would use cInt(n) to convert a string to an integer. How do you convert an integer to a string? I have a varChar field in a database that I am inserting a date field (needs to be a...
21
by: google | last post by:
I'm trying to implement something that would speed up data entry. I'd like to be able to take a string, and increment ONLY the right-most numerical characters by one. The type structure of the...
3
by: Yannick | last post by:
Hi, I try to execute request on a ms-access database but I have a problem with date. the "myDate" field's format is "date/time" my request is: SELECT myCode, myStuff, myDATE FROM myTable ...
26
by: jshanman | last post by:
I am writing a timeline that uses Google Maps. I have a function that converts a date time to latitude coords. This function is used to draw the markers on the timeline. I need a reverse function...
44
by: user | last post by:
Hi, Let's say I have 2 dates in the b/m format: Date 1 and date 2 How do I check whether Date2 is later than Date 1? Date1. 21-Nov-2006 09:00:00 PM
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
8
by: Steve Cartnal | last post by:
I have tried every function that seemed applicable and can't seem to convert a simple text field containing numbers, for example "022807", into a date. Nor can I do the reverse and convert a date...
10
by: Jes | last post by:
Dear all I have a date field on a HTML form where the user is asked to key in dd/mm/yyyy However, when that is written to MySql it is either not accepted or another value is tored in the...
144
by: dominantubergeek | last post by:
Hello, I'm a highly experienced expert C programmer and I've written this code to reverse a string in place. I think you could all learn something from it! int reverse(char* reverseme){ int...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shćllîpôpď 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.