Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

C# DateTime question...

  • 28-03-2009 08:02PM
    #1
    Closed Accounts Posts: 7,097 ✭✭✭


    I have a line of code that takes date time/timestamp from a form in the format "20090328185714", or YYYYDDMMhhmmss...

    TIMESTAMP.Text = Request.Form["TIMESTAMP"];

    At the moment, I'm putting this on my aspx page using a label:

    <asp:Label ID="TIMESTAMP2" runat="server" Width="685px" ForeColor="#ff3300" Font-Names="verdana" Font-Size="X-Small"></asp:Label>

    What I'd like to do is convert this from an unreadable "20090328185714" type word into something easier on the eye, like 27 March 2009 18:57PM or something more readable. Unfortunately the format of the date coming into my form from a remote server cannot be changed. It looks easy but everytime I try to do it, I keep getting back errors!


Comments

  • Registered Users, Registered Users 2 Posts: 2,931 ✭✭✭Ginger


    Darragh29 wrote: »
    I have a line of code that takes date time/timestamp from a form in the format "20090328185714", or YYYYDDMMhhmmss...

    TIMESTAMP.Text = Request.Form["TIMESTAMP"];

    At the moment, I'm putting this on my aspx page using a label:

    <asp:Label ID="TIMESTAMP2" runat="server" Width="685px" ForeColor="#ff3300" Font-Names="verdana" Font-Size="X-Small"></asp:Label>

    What I'd like to do is convert this from an unreadable "20090328185714" type word into something easier on the eye, like 27 March 2009 18:57PM or something more readable. Unfortunately the format of the date coming into my form from a remote server cannot be changed. It looks easy but everytime I try to do it, I keep getting back errors!

    Do you need the request timestamp, could you create your own one instead such as DateTime myDate = DateTime.Now.ToLongDateString(new CultureInfo.GetCurrentUICulture);

    You can possibly use a DateTime.TryParse(string) and see if it can return it back using a specific culture instance


    For example

    DateTime myDate;

    if(DateTime.TryParse(datestring, out myDate))
    {
    myDate.ToLongDateString(new CultureInfo.GetSpecificCulture("en-ie"));
    }


  • Closed Accounts Posts: 7,097 ✭✭✭Darragh29


    Thanks a mil Ginger. I posted in desparation last night when I was pulling my hair out with this!

    I finally got it sorted using the code below...

    TIMESTAMP.Text = Request.Form["TIMESTAMP"];
    string DTString = TIMESTAMP.Text;
    string DTFormat = "yyyyMMddHHmmss";
    DateTime time1 = DateTime.ParseExact(DTString, DTFormat, CultureInfo.InvariantCulture);
    TIMESTAMP.Text = string.Format("{0:r}", time1);


    I'm glad I stuck with asp.net and c# now, but every now and again, I run into a headwrecker issue!


  • Registered Users, Registered Users 2 Posts: 2,793 ✭✭✭John_Mc


    Darragh29 wrote: »
    Thanks a mil Ginger. I posted in desparation last night when I was pulling my hair out with this!

    I finally got it sorted using the code below...

    TIMESTAMP.Text = Request.Form["TIMESTAMP"];
    string DTString = TIMESTAMP.Text;
    string DTFormat = "yyyyMMddHHmmss";
    DateTime time1 = DateTime.ParseExact(DTString, DTFormat, CultureInfo.InvariantCulture);
    TIMESTAMP.Text = string.Format("{0:r}", time1);


    I'm glad I stuck with asp.net and c# now, but every now and again, I run into a headwrecker issue!

    Glad to hear that you've come around to the .net way of things! :)


Advertisement