简体   繁体   English

将字符串格式转换为 mm/dd/yyyy 中的日期时间

[英]Converting string format to datetime in mm/dd/yyyy

I have to convert string in mm/dd/yyyy format to datetime variable but it should remain in mm/dd/yyyy format.我必须将 mm/dd/yyyy 格式的字符串转换为日期时间变量,但它应该保留为 mm/dd/yyyy 格式。

string strDate = DateTime.Now.ToString("MM/dd/yyyy");

Please help.请帮忙。

You are looking for the DateTime.Parse() method ( MSDN Article )您正在寻找DateTime.Parse()方法( MSDN 文章

So you can do:所以你可以这样做:

var dateTime = DateTime.Parse("01/01/2001");

Which will give you a DateTime typed object.这将为您提供键入 object 的DateTime

If you need to specify which date format you want to use, you would use DateTime.ParseExact ( MSDN Article )如果您需要指定要使用的日期格式,您可以使用DateTime.ParseExactMSDN 文章

Which you would use in a situation like this (Where you are using a British style date format):你会在这种情况下使用(你使用英式日期格式的地方):

string[] formats= { "dd/MM/yyyy" }
var dateTime = DateTime.ParseExact("01/01/2001", formats, new CultureInfo("en-US"), DateTimeStyles.None);

You need an uppercase M for the month part.月份部分需要大写的M

string strDate = DateTime.Now.ToString("MM/dd/yyyy");

Lowercase m is for outputting (and parsing) a minute (such as h:mm ).小写m用于输出(和解析)一分钟(例如h:mm )。

eg a full date time string might look like this:例如,完整的日期时间字符串可能如下所示:

string strDate = DateTime.Now.ToString("MM/dd/yyyy h:mm");

Notice the uppercase/lowercase mM difference.注意大写/小写的mM差异。


Also if you will always deal with the same datetime format string, you can make it easier by writing them as C# extension methods.此外,如果您将始终处理相同的日期时间格式字符串,则可以通过将它们编写为 C# 扩展方法来使其更容易。

public static class DateTimeMyFormatExtensions
{
  public static string ToMyFormatString(this DateTime dt)
  {
    return dt.ToString("MM/dd/yyyy");
  }
}

public static class StringMyDateTimeFormatExtension
{
  public static DateTime ParseMyFormatDateTime(this string s)
  {
    var culture = System.Globalization.CultureInfo.CurrentCulture;
    return DateTime.ParseExact(s, "MM/dd/yyyy", culture);
  }
}

EXAMPLE: Translating between DateTime/string示例:在日期时间/字符串之间进行转换

DateTime now = DateTime.Now;

string strNow = now.ToMyFormatString();
DateTime nowAgain = strNow.ParseMyFormatDateTime();

Note that there is NO way to store a custom DateTime format information to use as default as in .NET most string formatting depends on the currently set culture, ie请注意,没有办法存储自定义DateTime格式信息以用作default ,如.NET大多数字符串格式取决于当前设置的文化,即

System.Globalization.CultureInfo.CurrentCulture.

The only easy way you can do is to roll a custom extension method.您可以做的唯一简单方法是滚动自定义扩展方法。

Also, the other easy way would be to use a different "container" or "wrapper" class for your DateTime, ie some special class with explicit operator defined that automatically translates to and from DateTime/string.此外,另一种简单的方法是为您的 DateTime 使用不同的“容器”或“包装器”class,即一些特殊的 class 定义了explicit operator ,可以自动转换为日期时间/字符串或从日期时间/字符串转换。 But that is dangerous territory.但那是危险的领域。

Solution解决方案

DateTime.Now.ToString("MM/dd/yyyy", CultureInfo.InvariantCulture)

I did like this我喜欢这个

var datetoEnter= DateTime.ParseExact(createdDate, "dd/mm/yyyy", CultureInfo.InvariantCulture);

You can change the format too by doing this您也可以通过这样做来更改格式

string fecha = DateTime.Now.ToString(format:"dd-MM-yyyy");

// this change the "/" for the "-" // 这将"/"更改为"-"

The following works for me.以下对我有用。

string strToday = DateTime.Today.ToString("MM/dd/yyyy");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将字符串值转换为 DateTime 时保留 MM/dd/yyyy 格式 - Retain MM/dd/yyyy format when converting string value to DateTime 将DateTime转换为字符串格式yyyy-MM-dd时出错 - Error converting DateTime to string format yyyy-MM-dd 使用dd / mm / yyyy格式将日期时间转换为字符串时出错 - Error when converting datetime to string using dd/MM/yyyy format 将 dd/MM/yyyy 输入转换为日期格式的 yyyy-MM-dd 而不是 varchar/datetime - Converting dd/MM/yyyy input to yyyy-MM-dd in date format not varchar/datetime 将日期时间字符串转换为 ddd MMM dd HH:mm:ss 'EST' yyyy 格式? - Converting DateTime string in ddd MMM dd HH:mm:ss 'EST' yyyy format? 在MVC应用程序中将Web Service DateTime字符串转换为MM / dd / yyyy格式 - Converting Web Service DateTime string to MM/dd/yyyy format in MVC application 为什么Convert.ChangeType()不将日期字符串(以DD / MM / yyyy格式)转换为datetime类型? - Why Convert.ChangeType() not converting a date string (in dd/MM/yyyy format) into datetime type? 从DateTime将过期日期转换为格式为dd / mm / yyyy 00:00:00的字符串 - Converting overdueDate from a DateTime to string in the format dd/mm/yyyy 00:00:00 在ASP.NET Mvc中将dd / MM / yyyy字符串格式日期转换为MM / dd / yyyy日期格式 - Converting dd/MM/yyyy string format date to MM/dd/yyyy date format in ASP.NET Mvc 在C#中将字符串dd-MM-yyyy转换为DateTime yyyy-MM-dd - Converting string dd-MM-yyyy to DateTime yyyy-MM-dd in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM