转换字符串格式在MM / DD / YYYY日期时间 [英] Converting string format to datetime in mm/dd/yyyy

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

问题描述

我要转换为mm / dd / yyyy格式字符串变量DATETIME,但它应该留在毫米/ 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 类型的对象。

如果您需要指定要使用的日期格式,你可以使用 DateTime.ParseExact 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天全站免登陆