locked
Date object format question RRS feed

  • Question

  • User-1584441573 posted

    HI,

     

    I have a string in the followin format: "Jan06", Feb12 and so forth.

     

    I need to be able to convert this string to a Date object and substract 7 days from it in order to do some validation.

     

    Any ideas how to convert the above string to a date object?

     

    Thanks in advance.

    Wednesday, February 18, 2009 5:37 PM

Answers

  • User-1760427068 posted

     

    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="VB_SubstringDate.aspx.vb" Inherits="VB_SubstringDate" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            
            <br /><br />
            
            <asp:Button ID="ConvertButton" runat="server" Text="Convert" 
                onclick="ConvertButton_Click" />
            
            <br /><br />
            
            <asp:Label ID="ResultsLabel" runat="server" Text="Label"></asp:Label>
        
        </div>
        </form>
    </body>
    </html>

     

     

    Partial Class VB_SubstringDate
        Inherits System.Web.UI.Page
    
        Protected Sub ConvertButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ConvertButton.Click
    
            Dim originalstring As String = TextBox1.Text
    
            Dim abbreviatedmonth As String = originalstring.Substring(0, 3)
    
            Dim month As String = "01"
    
            If abbreviatedmonth = "Feb" Then
                month = "02"
            End If
            If abbreviatedmonth = "Mar" Then
                month = "03"
            End If
            If abbreviatedmonth = "Apr" Then
                month = "04"
            End If
            If abbreviatedmonth = "May" Then
                month = "05"
            End If
            If abbreviatedmonth = "Jun" Then
                month = "06"
            End If
            If abbreviatedmonth = "Jul" Then
                month = "07"
            End If
            If abbreviatedmonth = "Aug" Then
                month = "08"
            End If
            If abbreviatedmonth = "Sep" Then
                month = "09"
            End If
            If abbreviatedmonth = "Oct" Then
                month = "10"
            End If
            If abbreviatedmonth = "Nov" Then
                month = "11"
            End If
            If abbreviatedmonth = "Dec" Then
                month = "12"
            End If
    
    
    
            Dim thedate As String = originalstring.Substring(3, 2)
    
            Dim minus7 As String = Convert.ToString(Convert.ToInt32(thedate) - 7)
    
            Dim yourtime As DateTime = Convert.ToDateTime(month + "/" + minus7 + "/2009")
    
            ResultsLabel.Text = "abbreviatedmonth is " + abbreviatedmonth + " and the date is " + thedate
            ResultsLabel.Text += "<br /><br />"
            ResultsLabel.Text += "the full DateTime is " + yourtime.ToString()
            ResultsLabel.Text += "<br /><br />"
            ResultsLabel.Text += "the shortDate is " + yourtime.ToShortDateString()
        End Sub
    End Class
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 18, 2009 10:11 PM

All replies

  • User-1760427068 posted

    Do you work with C# or VB?

    We can use .Substring(0,3) to get the Jan and .Substring (4,2) to get 06 of 12.

    Do we assume the year is 2009?

     I'll try an example in C# while waiting to hear back from you.

     I'll check on the internet to see if there is something that "knows" Jan is January--otherwise we could build something with 12 cases that converts over.

    Wednesday, February 18, 2009 9:37 PM
  • User-1760427068 posted

     

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="CS_SubStringDate.aspx.cs" Inherits="CS_SubStringDate" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            
            <br /><br />
            
            <asp:Button ID="ConvertButton" runat="server" Text="Convert" 
                onclick="ConvertButton_Click" />
            
            <br /><br />
            
            <asp:Label ID="ResultsLabel" runat="server" Text="Label"></asp:Label>
        
        </div>
        </form>
    </body>
    </html>

     

     

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    public partial class CS_SubStringDate : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
        protected void ConvertButton_Click(object sender, EventArgs e)
        {
            string originalstring = TextBox1.Text;
    
            string abbreviatedmonth = originalstring.Substring(0, 3);
    
            string month = "01";
    
            if (abbreviatedmonth == "Feb") { month = "02"; }
            if (abbreviatedmonth == "Mar") { month = "03"; }
            if (abbreviatedmonth == "Apr") { month = "04"; }
            if (abbreviatedmonth == "May") { month = "05"; }
            if (abbreviatedmonth == "Jun") { month = "06"; }
            if (abbreviatedmonth == "Jul") { month = "07"; }
            if (abbreviatedmonth == "Aug") { month = "08"; }
            if (abbreviatedmonth == "Sep") { month = "09"; }
            if (abbreviatedmonth == "Oct") { month = "10"; }
            if (abbreviatedmonth == "Nov") { month = "11"; }
            if (abbreviatedmonth == "Dec") { month = "12"; }
    
    
            string thedate = originalstring.Substring(3, 2);
    
            string minus7 = Convert.ToString(Convert.ToInt32(thedate) - 7);
    
            DateTime yourtime = Convert.ToDateTime(month + "/" + minus7 + "/2009");
    
            ResultsLabel.Text  = "abbreviatedmonth is " + abbreviatedmonth + " and the date is " + thedate;
            ResultsLabel.Text += "<br /><br />";
            ResultsLabel.Text += "the full DateTime is " + yourtime.ToString();
            ResultsLabel.Text += "<br /><br />";
            ResultsLabel.Text += "the shortDate is " + yourtime.ToShortDateString();
        }
    }
     
    Wednesday, February 18, 2009 9:55 PM
  • User-1760427068 posted

     

    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="VB_SubstringDate.aspx.vb" Inherits="VB_SubstringDate" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            
            <br /><br />
            
            <asp:Button ID="ConvertButton" runat="server" Text="Convert" 
                onclick="ConvertButton_Click" />
            
            <br /><br />
            
            <asp:Label ID="ResultsLabel" runat="server" Text="Label"></asp:Label>
        
        </div>
        </form>
    </body>
    </html>

     

     

    Partial Class VB_SubstringDate
        Inherits System.Web.UI.Page
    
        Protected Sub ConvertButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ConvertButton.Click
    
            Dim originalstring As String = TextBox1.Text
    
            Dim abbreviatedmonth As String = originalstring.Substring(0, 3)
    
            Dim month As String = "01"
    
            If abbreviatedmonth = "Feb" Then
                month = "02"
            End If
            If abbreviatedmonth = "Mar" Then
                month = "03"
            End If
            If abbreviatedmonth = "Apr" Then
                month = "04"
            End If
            If abbreviatedmonth = "May" Then
                month = "05"
            End If
            If abbreviatedmonth = "Jun" Then
                month = "06"
            End If
            If abbreviatedmonth = "Jul" Then
                month = "07"
            End If
            If abbreviatedmonth = "Aug" Then
                month = "08"
            End If
            If abbreviatedmonth = "Sep" Then
                month = "09"
            End If
            If abbreviatedmonth = "Oct" Then
                month = "10"
            End If
            If abbreviatedmonth = "Nov" Then
                month = "11"
            End If
            If abbreviatedmonth = "Dec" Then
                month = "12"
            End If
    
    
    
            Dim thedate As String = originalstring.Substring(3, 2)
    
            Dim minus7 As String = Convert.ToString(Convert.ToInt32(thedate) - 7)
    
            Dim yourtime As DateTime = Convert.ToDateTime(month + "/" + minus7 + "/2009")
    
            ResultsLabel.Text = "abbreviatedmonth is " + abbreviatedmonth + " and the date is " + thedate
            ResultsLabel.Text += "<br /><br />"
            ResultsLabel.Text += "the full DateTime is " + yourtime.ToString()
            ResultsLabel.Text += "<br /><br />"
            ResultsLabel.Text += "the shortDate is " + yourtime.ToShortDateString()
        End Sub
    End Class
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 18, 2009 10:11 PM
  • User-1584441573 posted

     I am using vb.net

     

    Thanks for the responces I will see if  I can convert you code to vb

    Thursday, February 19, 2009 8:31 AM
  • User-1760427068 posted

     I am using vb.net

    Thanks for the responces I will see if  I can convert you code to vb

     

    Please find a VB version posted after the C# version.

    Thursday, February 19, 2009 9:05 AM
  • User-1584441573 posted

     Thank you that did the trick.

     

    Appreciate you input and help.

    Thursday, February 19, 2009 10:18 AM