473,795 Members | 3,255 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4024
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("MMddy y").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*******@bigf oot.comwrote in message
news:OU******** *****@TK2MSFTNG P06.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(s tring pstrString)
{
char[] strArray = pstrString.ToCh arArray();
Array.Reverse(s trArray);
return new string(strArray );
}

Then do the following:

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

Sep 5 '07 #3

On Sep 5, 11:39 pm, Dan Holmes <danhol...@bigf oot.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.co mwrote in message
news:OR******** ******@TK2MSFTN GP06.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" );
datecodemont h = 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...String Builder!!!

--
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.co mwrote in message
news:OR******** ******@TK2MSFTN GP06.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" );
datecodemon th = datecodemonth[1] + datecodemonth[0];
string datecodeday = d.ToString("dd" );
datecodeday = datecodeday[1] + datecodeday[0];
string datecodeyear = d.ToString("yy" );
datecodeyea r = 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...String Builder!!!
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.co mwrote:
Doug Semler wrote:
"Göran Andersson" <gu...@guffa.co mwrote in message
news:OR******** ******@TK2MSFTN GP06.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" );
datecodemont h = 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...String Builder!!!

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("yyd dMM");
char[] c = original.ToChar Array();
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********@gma il.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.co m>
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

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

Similar topics

2
3204
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 char field). The date is formatted like so: y = year(now()) m = month(now()) d = day(now()) vNow = Y & "-" & M & "-" & D To me this looks like it should come up with
21
3987
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 data that is in this field can vary. It's a list of mechanical equipment, and how it is designated varies based on how the customer has them labeled. For example, a list of their equipment might look like: CH-1 CH-2 CH-3
3
2262
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 WHERE myDate = #05/21/2004# in ms-access interface, it works fine !
26
2729
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 to convert a latitude coord back to an accurate date time. Then I could detect the day/hour in the viewport when the timeline is zoomed in or out, or if it is moved. I cannot use the javascript date functions because this timeline will...
44
10236
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
5286
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
32439
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 field into text, like 02/28/07 into text "022807". Can anyone help?
10
2549
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 database. Is there any way to change value of this field back to yyyymmdd format as accepted correctly in sql. ? The change should preferably be when user click on submit.
144
5011
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 retval=-1; if(retval!=NULL){ int len=strlen(retval){ if(len>0){ int half=len>>1;
0
9672
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10438
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10164
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9042
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7540
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6780
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3727
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.