将字符串格式转换为日期时间,单位为mm / dd / yyyy [英] Converting string format to datetime in mm/dd/yyyy

查看:311
本文介绍了将字符串格式转换为日期时间,单位为mm / dd / yyyy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须将mm / dd / yyyy格式的字符串转换为datetime变量,但应保持为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.

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

请帮助。

推荐答案

您正在寻找 DateTime.Parse()方法( MSDN文章

所以你可以做:

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

哪个会给你一个 DateTime 键入的对象

Which will give you a DateTime typed object.

如果您需要指定要使用的日期格式,则可以使用 DateTime.ParseExact (< a href =http://msdn.microsoft.com/en-us/library/332de853.aspx =noreferrer> MSDN文章)

If you need to specify which date format you want to use, you would use DateTime.ParseExact (MSDN Article)

您将在这样的情况下使用(您使用英式格式的日期格式):

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

这篇关于将字符串格式转换为日期时间,单位为mm / dd / yyyy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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