去除“时间”从包含日期时间val的字符串变量中删除 [英] Strip the "time" off from a string variable holding a datetime val

查看:110
本文介绍了去除“时间”从包含日期时间val的字符串变量中删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串变量,它等同于DateTime值。我从数据库中提取了这个日期时间,我想在显示给用户之前剥掉时间

部分。


我正在使用C#


例如。

- 字符串变量" strMyDate"保持值1/1/2005 12:00:00 AM来自

数据库。

- 我不关心时间部分,我只想要1/1/2005。用于显示。


我可以对字符串变量strMyDate做什么?去除时间部分


我已经使用DateTime.Parse和ParseExact函数在圈子里跑了,我是

不知道如何最好的去做。它曾经在VB 6.0中如此简单,使用

格式方法。我怎样才能用C#来实现这个目标?

-

PK9

I have a string variable that holds the equivalent of a DateTime value. I
pulled this datetime from the database and I want to strip off the time
portion before displaying to the user.

I am using C#

eg.
- String variable "strMyDate" holds the value "1/1/2005 12:00:00 AM" from
the database.
- I do not care about the time portion, I only want "1/1/2005" for display.

What can I do to the string variable "strMyDate" to strip out the time portion

I''ve ran in circles using the DateTime.Parse and ParseExact functions, I''m
not sure how to best go about this. It used to be so easy in VB 6.0, using
the Format method. How can I accomplish this using C# ?
--
PK9

推荐答案



我认为这是C#代码:

DateTime D =(DateTime)(1/1/2005 12:00:00 AM)

MessageBox.Show(D.Date)


这将是VB代码(通常可以正常工作)

Dim D as DateTime = CDate(1/1/2005 12:00:00 AM)

MessageBox.Show(D.Date)


注意:我这样做了从记忆中没有测试过它。

Chris

" PK9" < PK*@discussions.microsoft.com>在消息中写道

新闻:EC ********************************** @ microsof t.com ...

I think this is the C# code:
DateTime D = (DateTime)("1/1/2005 12:00:00 AM")
MessageBox.Show(D.Date)

This would be it in VB code (which is what normally work on)
Dim D as DateTime = CDate("1/1/2005 12:00:00 AM")
MessageBox.Show(D.Date)

Note: I did this from memory and did not test it.
Chris
"PK9" <PK*@discussions.microsoft.com> wrote in message
news:EC**********************************@microsof t.com...
我有一个字符串变量,它等价于DateTime值。我从数据库中提取了这个日期时间,我想在显示给用户之前剥掉时间
部分。

我正在使用C#

例如。
- 字符串变量" strMyDate"保持值1/1/2005 12:00:00 AM来自
数据库。
- 我不关心时间部分,我只想要1/1 / 2005。用于
显示。

我可以对字符串变量strMyDate做什么?剥离时间


我使用DateTime.Parse和ParseExact函数在圈子里跑了,我不知道如何最好地解决这个问题。以前在VB 6.0中很容易使用
Format方法。如何使用C#实现这一目标?
-
PK9
I have a string variable that holds the equivalent of a DateTime value. I
pulled this datetime from the database and I want to strip off the time
portion before displaying to the user.

I am using C#

eg.
- String variable "strMyDate" holds the value "1/1/2005 12:00:00 AM" from
the database.
- I do not care about the time portion, I only want "1/1/2005" for
display.

What can I do to the string variable "strMyDate" to strip out the time
portion

I''ve ran in circles using the DateTime.Parse and ParseExact functions, I''m
not sure how to best go about this. It used to be so easy in VB 6.0,
using
the Format method. How can I accomplish this using C# ?
--
PK9



PK9< PK*@discussions.microsoft.com>写道:
PK9 <PK*@discussions.microsoft.com> wrote:
我有一个字符串变量,它等价于DateTime值。我从数据库中提取了这个日期时间,我想在显示给用户之前剥掉时间
部分。

我正在使用C#

例如。
- 字符串变量" strMyDate"保持值1/1/2005 12:00:00 AM来自
数据库。
- 我不关心时间部分,我只想要1/1 / 2005。用于显示。

我可以对字符串变量strMyDate做什么?剥离时间部分

我已经使用DateTime.Parse和ParseExact函数在圈子里跑了,我不知道如何最好地解决这个问题。以前在VB 6.0中使用
Format方法非常容易。如何使用C#完成此操作?
I have a string variable that holds the equivalent of a DateTime value. I
pulled this datetime from the database and I want to strip off the time
portion before displaying to the user.

I am using C#

eg.
- String variable "strMyDate" holds the value "1/1/2005 12:00:00 AM" from
the database.
- I do not care about the time portion, I only want "1/1/2005" for display.

What can I do to the string variable "strMyDate" to strip out the time portion

I''ve ran in circles using the DateTime.Parse and ParseExact functions, I''m
not sure how to best go about this. It used to be so easy in VB 6.0, using
the Format method. How can I accomplish this using C# ?




您需要使用DateTime.ParseExact将字符串转换为

DateTime,然后使用DateTime .ToString指定一个合适的格式

说明符只包含日期部分。


如果你能告诉我们你已经尝试过的东西,我们可以帮忙修复

it。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复小组,请不要给我发邮件



You need to use DateTime.ParseExact to convert the string to a
DateTime, and then DateTime.ToString specifying an appropriate format
specifier which only contains date parts.

If you could show us what you''ve already tried, we could help to fix
it.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


这是我正在努力摆脱的时间部分日期:


//这是日期从数据库回来的方式

string strDate =" 01/23/2005 12:00:00 AM"


IFormatProvider format = new System.Globalization.CultureInfo(" en-US",true);

string [] expectedF ormats = {" M / d / yyyy hh:mm:ss"};


DateTime NewDate = DateTime.ParseExact(DateWithTime,

expectedFormats,< br $>
格式,

System.Globalization.DateTimeStyles.AllowWhiteSpac es);


string sDateWithoutTime = NewDate.ToString(" M / d / yyyy);

我尝试了不同的选项,但是无法正常工作。


" Jon Skeet [ C#MVP]"写道:
Here is what I''m trying in order to get rid of the time portion of the date:

//THIS IS THE WAY THE DATE COMES BACK FROM THE DATABASE
string strDate = "01/23/2005 12:00:00 AM";

IFormatProvider format = new System.Globalization.CultureInfo("en-US", true);
string[] expectedFormats = {"M/d/yyyy hh:mm:ss"};

DateTime NewDate = DateTime.ParseExact(DateWithTime,
expectedFormats,
format,
System.Globalization.DateTimeStyles.AllowWhiteSpac es);

string sDateWithoutTime = NewDate.ToString("M/d/yyyy");
I''ve tried different options, but can''t get it to work correctly.

"Jon Skeet [C# MVP]" wrote:
PK9< PK*@discussions.microsoft.com>写道:
PK9 <PK*@discussions.microsoft.com> wrote:
我有一个字符串变量,它等价于DateTime值。我从数据库中提取了这个日期时间,我想在显示给用户之前剥掉时间
部分。

我正在使用C#

例如。
- 字符串变量" strMyDate"保持值1/1/2005 12:00:00 AM来自
数据库。
- 我不关心时间部分,我只想要1/1 / 2005。用于显示。

我可以对字符串变量strMyDate做什么?剥离时间部分

我已经使用DateTime.Parse和ParseExact函数在圈子里跑了,我不知道如何最好地解决这个问题。以前在VB 6.0中使用
Format方法非常容易。如何使用C#完成此操作?
I have a string variable that holds the equivalent of a DateTime value. I
pulled this datetime from the database and I want to strip off the time
portion before displaying to the user.

I am using C#

eg.
- String variable "strMyDate" holds the value "1/1/2005 12:00:00 AM" from
the database.
- I do not care about the time portion, I only want "1/1/2005" for display.

What can I do to the string variable "strMyDate" to strip out the time portion

I''ve ran in circles using the DateTime.Parse and ParseExact functions, I''m
not sure how to best go about this. It used to be so easy in VB 6.0, using
the Format method. How can I accomplish this using C# ?



您需要使用DateTime.ParseExact将字符串转换为
DateTime,然后使用DateTime.ToString指定适当的格式
说明符只包含日期部分。

如果你能告诉我们你已经尝试过的东西,我们可以帮忙修复它。

- - Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet
如果回复小组,请不要给我发邮件



You need to use DateTime.ParseExact to convert the string to a
DateTime, and then DateTime.ToString specifying an appropriate format
specifier which only contains date parts.

If you could show us what you''ve already tried, we could help to fix
it.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too



这篇关于去除“时间”从包含日期时间val的字符串变量中删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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