Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am working on an application to track student attendance an similar information, and to simplify the registration of the typing of dates. I've gone with writing YYYYMMDD to input dates, (ex: 20140429 - without any punctuation marks, as it's simple to handle and has it's advantages in terms of sorting). However, the machine don't know this is a date and so having the option to using a "datepicker" seems difficult.

I want to store the date as a number in the database, (I might use a SQLite, access or CSV -file and a simple sortable date/number is great in this situation). I have done some Google-ing, but I don't really know where to start; should I use "string manipulation"? Are there some functionality in the DateTime in .net that I am missing?

Any help would be appreciated!

-Frank
Posted

you can use DateTime.TryParseExact method to convert your input text to date time
C#
string str = "20140429";
string[] format = {"yyyyMMdd"};
DateTime date;

if (DateTime.TryParseExact(str, 
                           format, 
                           System.Globalization.CultureInfo.InvariantCulture,
                           System.Globalization.DateTimeStyles.None, 
                           out date))
{
     //valid and insert in to database as DateTime value 
}


DateTime.TryParseExact method return true if your input string in correct format. don't save your date time as integer or text field in database. Save it as Date or DateTime.
 
Share this answer
 
v3
Comments
[no name] 29-Apr-14 3:25am    
I hope this is the solution
Frank R. Haugen 29-Apr-14 5:38am    
Mostly it's directed towards being used in a CSV, (it's for a customer, and customers are... customers)

I got just what I needed, thanks! Though I tweaked it just a bit, to convert back again to string, (if I or someone else should need it; you never know)
I used DamithSL's solution, with a tweak, (I keep small console applications laying around for reference), and added a way to return the value to a string.

Hope this can be useful to someone else :-D
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ParseDateTimeYYYYMMDDconsole
{
    class Program
    {
        static void Main(string[] args)
        {

            string str = "20140429";
            string[] format = { "yyyyMMdd" };
            DateTime date;
            string ReStringed;

            if (DateTime.TryParseExact(str,
                           format,
                           System.Globalization.CultureInfo.InvariantCulture,
                           System.Globalization.DateTimeStyles.None,
                           out date))
            {

                Console.WriteLine("The raw string of YYYYMMDD date:");
                Console.WriteLine(str);

                Console.WriteLine("Date given after conversion to DateTime:");
                Console.WriteLine(date);

                ReStringed = date.ToString("yyyyMMdd");

                Console.WriteLine("The date re-converted
                                        back to a string, formatted yyyyMMdd:");
                Console.WriteLine(ReStringed);

                Console.ReadLine();
            }
        }
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900