Background: I have a tool that grabs files from a network location based on the file name. There are various files, but the names are all based on the creation date (report052209.txt).

What i need to do is take the date selected from a datetimepicker and save a two digit day, month, and year to seperate variables.

I can't for the life of me get this to work. Just for the sake of practice I've tried creating a form with just a date picker, a button, and a textbox and writting code that will either on button.click display a two digit month in the textbox, or store the two digit month in a variable and display the variable in the text box.

I still want the datetimepicker to display the full date for the user's convenience.

Any help is appreciated.
Thanks

Recommended Answers

All 2 Replies

You can use the Format$ function to pick out individual bits of a date, see code below.

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">


    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        TextBox1.Text = Format$(Calendar1.SelectedDate, "dd")
        TextBox2.Text = Format$(Calendar1.SelectedDate, "MM")
        TextBox3.Text = Format$(Calendar1.SelectedDate, "yy")
        
    End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Calendar ID="Calendar1" runat="server" ></asp:Calendar>
    
    </div>
        <asp:Label ID="Label1" runat="server" Text="Day"></asp:Label>
        <asp:TextBox ID="TextBox1" runat="server" Width="58px"></asp:TextBox>
        <asp:Label ID="Label2" runat="server" Text="Month"></asp:Label>
        <asp:TextBox ID="TextBox2" runat="server" Width="61px"></asp:TextBox>
        <asp:Label ID="Label3" runat="server" Text="Year"></asp:Label>
        <asp:TextBox ID="TextBox3" runat="server" Width="47px"></asp:TextBox><br />
        <asp:Button ID="Button1" runat="server" Text="Update" OnClick="Button1_Click" />
    </form>
</body>
</html>

ToString() method,

str1=DateTimePicker1.Value.ToString("dd") 
 str2=DateTimePicker1.Value.ToString("MM")
 str3=DateTimePicker1.Value.ToString("yy")
commented: Thanks for the answer - I think I was making it too complicated. +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.