如何从字符串中的日期时间仅获取月份和日期 [英] how to get only month and date from datetime in string

查看:747
本文介绍了如何从字符串中的日期时间仅获取月份和日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我获取DateTime的字符串是这样的:

 字符串 myDate = System.DateTime.Now.ToString("  MM/dd/yyyy"); 


如果要获取月份日期的所有值,如何从字符串上方排除日期.
请提供代码帮助.

问候,
bishnu

解决方案

以格式字符串编写的内容将从日期中提取.

 System.DateTime.Now.ToString(" );
System.DateTime.Now.ToString(" );
System.DateTime.Now.ToString(" ); 


尝试如下:

DateTime myDateTime = DateTime.Now;
       string month = myDateTime.Month.ToString();
       string year = myDateTime.Year.ToString();


使用此

 字符串 myDate = System.DateTime.Now.ToString("  MM yy"); 


my string for getting DateTime is like this:

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


If I want to obtain all values of dates of month, how can I exclude the day from above string.
Please help with the code.

With regards,
bishnu

解决方案

whatever you will write in format string will be extracted from the date like.

System.DateTime.Now.ToString("MM yy");
System.DateTime.Now.ToString("MMM yyyy");
System.DateTime.Now.ToString("MM/yyyy");


try as follows:

DateTime myDateTime = DateTime.Now;
       string month = myDateTime.Month.ToString();
       string year = myDateTime.Year.ToString();


use this

string myDate = System.DateTime.Now.ToString("MM yy");


这篇关于如何从字符串中的日期时间仅获取月份和日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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