473,804 Members | 3,021 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
11 4028
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
3205
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
3989
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
2263
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
2733
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
10243
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
32440
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
2552
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
5021
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
9706
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
10583
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...
0
10337
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10323
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
10082
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9160
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
7622
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
5654
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4301
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.