从c#.net中的日期字符串获取月份 [英] get month from date string in c#.net

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

问题描述

你好,
我正在使用C#,

.aspx的代码
< asp:GridView ID ="GridView1" runat ="server" AutoGenerateColumns ="False" DataKeyNames ="id"
OnPageIndexChanging ="GridView1_PageIndexChanging"
OnRowCommand ="GridView1_RowCommand" PageSize ="10"
class ="table table-striped table-bordered datatables"
AllowPaging ="True">
<列> < asp:TemplateField HeaderText =员工姓名">
< itemtemplate>
< asp:标签ID ="lblempname" runat ="server" Text =''<%#Eval("empname")%>''/>


< asp:TemplateField HeaderText =工作类型">
< itemtemplate>
< asp:标签ID ="lblworktype" runat =服务器" Text =''<%#Eval("worktype")%>''/>


< asp:TemplateField HeaderText =公司名称">
< itemtemplate>
< asp:标签ID ="lblcompanyname" runat =服务器" Text =''<%#Eval("companyname")%>''/>



< asp:TemplateField HeaderText ="Date">
< itemtemplate>
< asp:TextBox ID ="txtdate" runat ="server" class ="form-control"> < asp:CalendarExtender ID ="txtdate_CalendarExtender" runat ="server" Enabled ="True" TargetControlID ="txtdate" Format ="dd/MM/yyyy">




< asp:TemplateField HeaderText =开始时间">
< itemtemplate>
< asp:TextBox ID ="txtstime" class ="timepicker form-control" runat ="server">


< asp:TemplateField HeaderText =结束时间">
< itemtemplate>
< asp:TextBox ID ="txtetime" class ="timepicker form-control" runat ="server">


< asp:TemplateField HeaderText =考勤符号">
< itemtemplate>
< asp:DropDownList ID ="ddlattsysmbol" runat ="server" class ="form-control" DataSourceID ="SqlDataSource1"
DataTextField =短名称" DataValueField ="id" AppendDataBoundItems ="true">
< asp:ListItem Value =">选择任何一个

< asp:SqlDataSource ID ="SqlDataSource1" runat =服务器" ConnectionString =<%$ ConnectionStrings:HRMConnectionString%>"
SelectCommand ="SELECT DISTINCT * FROM [sysmbol_attend]在哪里([cid] = @cid)">
< SelectParameters>
< asp:SessionParameter Name ="cid" SessionField ="CompanyID" Type ="Int32"/>
</SelectParameters>



< asp:TemplateField>
< itemtemplate>
< asp:ImageButton ID ="imgbtnAdd" runat ="server" ImageUrl =〜/images/AddNewitem.jpg"
CommandName ="AddNew" Width ="30px" Height ="30px" ToolTip =添加新用户" ValidationGroup ="a"/>


< pagersettings mode ="NextPreviousFirstLast" pagebuttoncount ="4" previouspagetext =上一页">
NextPageText ="Next" FirstPageText ="First" LastPageText ="Last"/>


.cs
受保护的void GridView1_RowCommand(对象发送者,GridViewCommandEventArgs e)
{
如果(e.CommandName =="AddNew")
{
GridViewRow row =(GridViewRow)(((ImageButton)e.CommandSource).NamingContainer;
字符串curdate =(row.FindControl("txtdate")as TextBox).Text;
}

在curdate字符串中,我得到的输出是''2014/07/29'',我也想从我的currate字符串输出中获得月份,例如''07'' .

Hello,
I am working on C#,

code for .aspx
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="id"
OnPageIndexChanging="GridView1_PageIndexChanging"
OnRowCommand="GridView1_RowCommand" PageSize="10"
class="table table-striped table-bordered datatables"
AllowPaging="True" >
<columns> <asp:TemplateField HeaderText="Employee Name">
<itemtemplate>
<asp:Label ID="lblempname" runat="server" Text=''<%#Eval("empname") %>'' />


<asp:TemplateField HeaderText="Work Type">
<itemtemplate>
<asp:Label ID="lblworktype" runat="server" Text=''<%#Eval("worktype") %>'' />


<asp:TemplateField HeaderText="Company Name">
<itemtemplate>
<asp:Label ID="lblcompanyname" runat="server" Text=''<%#Eval("companyname") %>'' />



<asp:TemplateField HeaderText="Date">
<itemtemplate>
<asp:TextBox ID="txtdate" runat="server" class="form-control"> <asp:CalendarExtender ID="txtdate_CalendarExtender" runat="server" Enabled="True" TargetControlID="txtdate" Format="dd/MM/yyyy">




<asp:TemplateField HeaderText="Start Time">
<itemtemplate>
<asp:TextBox ID="txtstime" class="timepicker form-control" runat="server">


<asp:TemplateField HeaderText="End Time">
<itemtemplate>
<asp:TextBox ID="txtetime" class="timepicker form-control" runat="server" >


<asp:TemplateField HeaderText="Attendance Symbol">
<itemtemplate>
<asp:DropDownList ID="ddlattsysmbol" runat="server" class="form-control" DataSourceID="SqlDataSource1"
DataTextField="shortname" DataValueField="id" AppendDataBoundItems="true">
<asp:ListItem Value="">Select Any One

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:HRMConnectionString %>"
SelectCommand="SELECT DISTINCT * FROM [sysmbol_attend] WHERE ([cid] = @cid)">
<SelectParameters>
<asp:SessionParameter Name="cid" SessionField="CompanyID" Type="Int32" />
</SelectParameters>



<asp:TemplateField>
<itemtemplate>
<asp:ImageButton ID="imgbtnAdd" runat="server" ImageUrl="~/images/AddNewitem.jpg"
CommandName="AddNew" Width="30px" Height="30px" ToolTip="Add new User" ValidationGroup="a" />


<pagersettings mode="NextPreviousFirstLast" pagebuttoncount="4" previouspagetext="Previous">
NextPageText="Next" FirstPageText="First" LastPageText="Last" />


.cs
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "AddNew")
{
GridViewRow row = (GridViewRow)((ImageButton)e.CommandSource).NamingContainer;
String curdate = (row.FindControl("txtdate") as TextBox).Text;
}

In curdate string i am getting the output is ''29/07/2014'' , I want to also get month from my curdate string output like ''07''.

推荐答案

ConnectionStrings:HRMConnectionString%>"
SelectCommand ="SELECT DISTINCT * FROM [sysmbol_attend]在哪里([cid] = @cid)">
< SelectParameters>
< asp:SessionParameter Name ="cid" SessionField ="CompanyID" Type ="Int32"/>
</SelectParameters>



< asp:TemplateField>
< itemtemplate>
< asp:ImageButton ID ="imgbtnAdd" runat ="server" ImageUrl =〜/images/AddNewitem.jpg"
CommandName ="AddNew" Width ="30px" Height ="30px" ToolTip =添加新用户" ValidationGroup ="a"/>


< pagersettings mode ="NextPreviousFirstLast" pagebuttoncount ="4" previouspagetext =上一页">
NextPageText ="Next" FirstPageText ="First" LastPageText ="Last"/>


.cs
受保护的void GridView1_RowCommand(对象发送者,GridViewCommandEventArgs e)
{
如果(e.CommandName =="AddNew")
{
GridViewRow row =(GridViewRow)(((ImageButton)e.CommandSource).NamingContainer;
字符串curdate =(row.FindControl("txtdate")as TextBox).Text;
}

在curdate字符串中,我得到的输出是''2014/07/29'',我也想从我的currate字符串输出中获得月份,例如''07''
ConnectionStrings:HRMConnectionString %>"
SelectCommand="SELECT DISTINCT * FROM [sysmbol_attend] WHERE ([cid] = @cid)">
<SelectParameters>
<asp:SessionParameter Name="cid" SessionField="CompanyID" Type="Int32" />
</SelectParameters>



<asp:TemplateField>
<itemtemplate>
<asp:ImageButton ID="imgbtnAdd" runat="server" ImageUrl="~/images/AddNewitem.jpg"
CommandName="AddNew" Width="30px" Height="30px" ToolTip="Add new User" ValidationGroup="a" />


<pagersettings mode="NextPreviousFirstLast" pagebuttoncount="4" previouspagetext="Previous">
NextPageText="Next" FirstPageText="First" LastPageText="Last" />


.cs
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "AddNew")
{
GridViewRow row = (GridViewRow)((ImageButton)e.CommandSource).NamingContainer;
String curdate = (row.FindControl("txtdate") as TextBox).Text;
}

In curdate string i am getting the output is ''29/07/2014'' , I want to also get month from my curdate string output like ''07''.


您可以简单地使用string.SubString-这是微不足道的-但不要.
取而代之的是,使用DateTime.TryParse和用户区域性解析日期输入,以获取DateTime,并从中获取月份.
You could simply use string.SubString - that''s trivial - but don''t.
Instead, parse the date input using DateTime.TryParse and the user culture to get a DateTime, and get the month from that.


首先阅读Sergey Alexandrovich Kryukov的解决方案(解决方案2),然后如果您仍然选择弦乐而不是尝试我的解决方案.. :)

First read solution of Sergey Alexandrovich Kryukov (Solution 2) and than if you still go woth string than try my solution.. :)

string curdate = "29/07/2014";

string Month = curdate.Split('/')[1].ToString();


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

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