How to Convert Dates to Strings

Written by jim campbell
  • Share
  • Tweet
  • Share
  • Email

Each programming language has its own functions to convert date variables to strings. In some cases, you need to convert a date to a string variable to avoid a syntax error. The PHP, VB.NET and C# languages are online programming languages that allow you to create dynamic Web pages. Both VB.NET and C# use the "ToString" function to perform the conversion. PHP requires you to convert the string to a long date string.

Skill level:
Moderate

Other People Are Reading

Instructions

    C#

  1. 1

    Click the Windows "Start" button and type "visual studio" in the search text box. Press "Enter" to open the C# programming software.

  2. 2

    Open your Web project, then open the code file for the C# form. Use the following code to create a date value, if you do not already have one:

    DateTime mydate = new DateTime;

    mydate = DateTime.Now;

  3. 3

    Convert the date to a string. The following code changes the date created in Step 2 to a string:

    string stringvar = string.Empty;

    stringvar = mydate.ToString();

    VB.NET

  1. 1

    Click the Windows "Start" button and select "All Programs." Click "Microsoft .NET Framework," then click "Visual Studio."

  2. 2

    Open your VB.NET project and load the code file to set up the date-to-string conversion. Use the following code to create a date in VB.NET:

    Dim myvar As DateTime

    myvar = "2/1/2011"

  3. 3

    Convert the date created in step to to a string. The following code performs the conversion in VB.NET:

    Dim newvar As String

    newvar = myvar.ToString()

    PHP

  1. 1

    Right-click the PHP file you want to edit and select "Open With." Double-click your PHP file editor in the list of programs.

  2. 2

    Create a date variable, if you do not already have a variable set up in the PHP file. The following code shows you how to create a date in PHP:

    $date = '1/1/2011';

  3. 3

    Convert the date to a string. Use the following code to convert the date:

    $newstring = date('l dS \o\f F Y h:i:s A', $date);

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.