日期代码和字符串反向 [英] date code and string reverse

查看:67
本文介绍了日期代码和字符串反向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将日期反转以显示为日期代码。例如今天

,090507将被编码为905070(扭转部分日期。


这就是我所拥有的但是必须有一个更好的方式。


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;


想法?日期格式有什么可以做到的吗?


dan

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

推荐答案

似乎想要做一件非常奇怪的事情,但为什么不呢?-p

如果这是高音量,然后避免大量的中间字符串,

尝试在char []缓冲区中工作,有些比如:


static void Main(string [] args){

char [] buffer =

DateTime.Today。 ToString(" MMddyy")。ToCharArray();

交换(缓冲区,0,1); //也许展开...

交换(缓冲区,2,3);

交换(缓冲区,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;

}

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;
}


" Dan Holmes" < da ******* @ bigfoot.comwrote in message

news:OU ************* @ TK2MSFTNGP06.phx.gbl ...
"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?



首先,创建一个函数来反转字符串:


string StringReverse(string pstrString)

{

char [] strArray = pstrString.ToCharArray();

Array.Reverse(strArray);

返回新字符串(strArray );

}


然后执行以下操作:


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

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




开9月5日晚上11点39分,Dan Holmes< danhol ... @ bigfoot.comwrote:

On Sep 5, 11:39 pm, Dan Holmes <danhol...@bigfoot.comwrote:

我需要将日期反转以显示为日期代码。例如今天

,090507将编码为905070(扭转日期部分。
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.



Mark'和Marc'示例使用默认的DateTime字符串格式

提供程序,然后操纵其结果。更多.NET-ty解决方案

可能是实现自定义格式提供程序,并且使用它从DateTime而不是默认提供者创建

a字符串。这将是

避免首先转到090507然后转到905070,直接转到
$来自DateTime实例的b $ b 905070。有关如何实现自定义格式提供程序的示例,请参阅此处: http://www.codeproject.com/csharp/custstrformat.asp

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


这篇关于日期代码和字符串反向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆