Формат DateTime в C # - PullRequest
       10

Формат DateTime в C #

3 голосов
/ 04 февраля 2011

как мне отформатировать дату для отображения в следующем формате 03-Feb-2011

Я попробовал это утверждение, но оно не сработало должным образом

string.Format(visitDate.Value.ToShortDateString(), "dd-MMM-yyyy")

Ответы [ 4 ]

8 голосов
/ 04 февраля 2011
visitDate.Value.ToString("dd-MMM-yyyy"); 

Предполагается, что visitDate является датой времени.

5 голосов
/ 04 февраля 2011

вы должны использовать:

visitDate.Value.ToString("dd-MMM-yyyy");
1 голос
/ 04 февраля 2011

string.format действителен, но порядок используемых вами параметров недействителен:

DateTime? visitDate = null;
System.Diagnostics.Debug.WriteLine(visitDate == null ? "" : String.Format("{0:dd-MMM-yyyy}", visitDate));

visitDate = DateTime.Now;
System.Diagnostics.Debug.WriteLine(visitDate == null ? "" : String.Format("{0:dd-MMM-yyyy}", visitDate));

Это должно сработать.

0 голосов
/ 05 декабря 2013

DataTier.ClsGlobal dataBaseObj = new DataTier.ClsGlobal ();

    string strmessage;
    int intResult;

    public string login(string username, string password)// login method
    {
        dataBaseObj.sqlconn.Open();

        dataBaseObj.sqlCmd = new SqlCommand("SELECT  clEmail, clPassword FROM Client where clEmail='" + username + "'", dataBaseObj.sqlconn);
        dataBaseObj.sqlDr = dataBaseObj.sqlCmd.ExecuteReader();


        if (dataBaseObj.sqlDr.Read())
        {
            if (dataBaseObj.sqlDr["clPassword"].Equals(password.ToString()))
            {
                strmessage = "client";
                dataBaseObj.sqlconn.Close();
            }
            else
            {
                intResult++;

                strmessage = "login  not succesfull";

                dataBaseObj.sqlconn.Close();

                if (intResult == 3)
                {
                    strmessage = "your Blocked";
                }

            }
        }    // if its not the client is gonna go to workers table 
        else
        {
            dataBaseObj.sqlCmd = new SqlCommand("SELECT wuUsername, wuPassword, wuUserType FROM  WorkUsers where wuUsername'" + username + "'", dataBaseObj.sqlconn);
            dataBaseObj.sqlDr = dataBaseObj.sqlCmd.ExecuteReader();

            if (dataBaseObj.sqlDr.Read())
            {
                if (dataBaseObj.sqlDr["wuPassword"].Equals(password.ToString()))
                {
                    strmessage = "Receptionist";
                    dataBaseObj.sqlconn.Close();
                }
                else
                {

                    intResult++;

                    strmessage = "login  not succesful";

                    dataBaseObj.sqlconn.Close();

                    if (intResult == 3)
                    {
                        strmessage = "your Blocked";
                    }
                }
            }
        }
        return strmessage;
    }

    // this method is for registering the client of the client if not registered
    public string registration(string clID, string clFirstName, string clSurname, string clStreetAddress, string clCity, string clPostCode, string clHomePhone, string clMobilePhone, string clEmail, string clPassword, string clStatus)
    {
        dataBaseObj.sqlconn.Open();
        dataBaseObj.sqlCmd = new SqlCommand("insert into Client values(@clID,@clFirstName@clSurname,@clStreetAddress,@clCity,@clPostCode,@clHomePhone,@clMobilePhone,@clEmail,@clPassword@clStatus)", dataBaseObj.sqlconn);

        dataBaseObj.sqlCmd.Parameters.AddWithValue("@clID", clID);

        dataBaseObj.sqlCmd.Parameters.AddWithValue("@clFirstName", clFirstName);
        dataBaseObj.sqlCmd.Parameters.AddWithValue("@clSurname", clSurname);


        dataBaseObj.sqlCmd.Parameters.AddWithValue("@clStreetAddress", clStreetAddress);

        dataBaseObj.sqlCmd.Parameters.AddWithValue("@clCity", clCity);
        dataBaseObj.sqlCmd.Parameters.AddWithValue("@clPostCode", clPostCode);
        dataBaseObj.sqlCmd.Parameters.AddWithValue("@clHomePhone", clHomePhone);
        dataBaseObj.sqlCmd.Parameters.AddWithValue("@clMobilePhone", clMobilePhone);
        dataBaseObj.sqlCmd.Parameters.AddWithValue("@clEmail", clEmail);
        dataBaseObj.sqlCmd.Parameters.AddWithValue("@clPassword", clPassword);
        dataBaseObj.sqlCmd.Parameters.AddWithValue("@clStatus", clStatus);

        intResult = dataBaseObj.sqlCmd.ExecuteNonQuery();
        if (intResult == 1)
        {
            strmessage = "registeed";
        }
        else
        {
            strmessage = "not registeed";
        }

        return strmessage;
    }

    // this is for the admin to register the the receptionist
    public string AdminRegister(string Id, string username, string password)
    {
        dataBaseObj.sqlconn.Open();
        dataBaseObj.sqlCmd = new SqlCommand("INSERT INTO WorkUsers (wuID, wuUsername, wuPassword) VALUES ('" + Id + "','" + username + "','" + password + "')", dataBaseObj.sqlconn);
        intResult = intResult = dataBaseObj.sqlCmd.ExecuteNonQuery();
        if (intResult == 1)
        {
            strmessage = "registeed";
        }
        else
        {
            strmessage = "not registeed";
        }

        return strmessage;
    }

    // this method is fo receptionist to inser an appoirment
    public string ReceptioInsert(int appID, string appDateNow, string appTimeNow, string appReason, string appDateofBooking, string appTimeofBooking, int clID, int wuID)
    {
        dataBaseObj.sqlconn.Open();

       // appDateNow = Convert.ToString(System.DateTime.Now.Year + "/" + System.DateTime.Now.Month + "/" + System.DateTime.Now.DayOfWeek);
        appDateNow = Convert.ToString(System.DateTime.Now.ToString("m"));
        //appTimeNow = Convert.ToString(System.DateTime.Now.TimeOfDay);
        appTimeNow = Convert.ToString(System.DateTime.Now.ToString("hh:mm:ss"));

        dataBaseObj.sqlCmd = new SqlCommand("SELECT appDateNow, appTimeNow FROM  Appointments where appDateNow='" + appTimeNow + "'AND  where appTimeNow='" + appTimeNow + "'", dataBaseObj.sqlconn);
        if (dataBaseObj.sqlDr.Read())// if it reads it means date and time are booked
        {
            strmessage = "sorry that time is not available";
        }
        else
        {

            dataBaseObj.sqlCmd = new SqlCommand("INSERT INTO Appointments (appID, appDateNow, appTimeNow, appReason, appDateofBooking, appTimeofBooking, clID, wuID) VALUES('" + appID + "','" + appDateNow + "','" + appTimeNow + "','" + appReason + "','" + appDateofBooking + "','" + appTimeofBooking + "','" + clID + "','" + wuID + "')", dataBaseObj.sqlconn);
            intResult = intResult = dataBaseObj.sqlCmd.ExecuteNonQuery();
            if (intResult == 1)
            {
                strmessage = "registeed";
            }
            else
            {
                strmessage = "not registeed";
            }
            dataBaseObj.sqlconn.Close();
        }
        return strmessage;

    }
    // this method is to update the appointment
    public void updateAppointmen(int appID, string appDateNow, string appTimeNow, string appReason, string appDateofBooking, string appTimeofBooking, int clID, int wuID)
    {
        dataBaseObj.sqlconn.Open();
        appDateNow = Convert.ToString(System.DateTime.Now.Year + "/" + System.DateTime.Now.Month + "/" + System.DateTime.Now.DayOfWeek);

        appTimeNow = Convert.ToString(System.DateTime.Now.TimeOfDay);
        dataBaseObj.sqlCmd = new SqlCommand("UPDATE Appointments SET  appDateNow =@appDateNow, appTimeNow =@appTimeNow, appReason =@appReason, appDateofBooking =@appDateofBooking, appTimeofBooking =@appTimeofBooking, clID =@clID, wuID =@wuID where appID='" + appID + "'", dataBaseObj.sqlconn);



        dataBaseObj.sqlCmd.Parameters.AddWithValue("@appDateNow", appDateNow);
        dataBaseObj.sqlCmd.Parameters.AddWithValue("@appTimeNow", appTimeNow);

        dataBaseObj.sqlCmd.Parameters.AddWithValue("@appReason", appReason);
        dataBaseObj.sqlCmd.Parameters.AddWithValue("@appDateofBooking", appDateofBooking);

        dataBaseObj.sqlCmd.Parameters.AddWithValue("@appTimeofBooking", appTimeofBooking);
        dataBaseObj.sqlCmd.Parameters.AddWithValue("@clID", clID);
        dataBaseObj.sqlCmd.Parameters.AddWithValue("@wuID", wuID);
        dataBaseObj.sqlCmd.ExecuteNonQuery();
    }
    // this method is to delete the appoinment 

    public void deleteAppointment(int appID)
    {
        dataBaseObj.sqlconn.Open();
        dataBaseObj.sqlCmd = new SqlCommand("DELETE FROM Appointments whre appID='" + appID + "'");
        dataBaseObj.sqlCmd.ExecuteNonQuery();
    }
    // method for client to view the appoinments
    public DataTable CLientViewAppoitnmet(string date)
    {
        dataBaseObj.sqlconn.Open();
        dataBaseObj.sqlAdt = new SqlDataAdapter("SELECT appID, appDateNow, appTimeNow, appReason, appDateofBooking, appTimeofBooking, clID, wuID FROM Appointments where appDateNow='" + date + "'", dataBaseObj.sqlconn);
        dataBaseObj.dt = new DataTable("appoitment");
        dataBaseObj.sqlAdt.Fill(dataBaseObj.dt);
        dataBaseObj.sqlconn.Close();
        return dataBaseObj.dt;
    }

// этот метод для клиента, чтобы вставить заметку открытая строка CLinetInformRecep (int anID, int appID, строка anNotes) { dataBaseObj.sqlconn.Open (); dataBaseObj.sqlCmd = new SqlCommand ("INSERT INTO AppointmentNotes (anID, appID, anNotes) ЗНАЧЕНИЯ ('" + anID + "', '" + appID + "', '" + anNotes + "')", dataBaseObj.sqlconn) ; intResult = intResult = dataBaseObj.sqlCmd.ExecuteNonQuery (); if (intResult == 1) { strmessage = "registeed"; } еще { strmessage = "не зарегистрирован"; } dataBaseObj.sqlconn.Close (); обратное сообщение; } // это для приема, чтобы просмотреть заметки, добавленные клиентом

    public DataTable selectAppointmentNotes()
    {


        dataBaseObj.sqlconn.Open();
        dataBaseObj.sqlAdt = new SqlDataAdapter("SELECT FROM AppointmentNotes", dataBaseObj.sqlconn);
        dataBaseObj.dt = new DataTable("appoitment");
        dataBaseObj.sqlAdt.Fill(dataBaseObj.dt);
        dataBaseObj.sqlconn.Close();
        return dataBaseObj.dt;
    }
    // this methiod is for therapist to view cuurent day appointment
    public DataTable TherapisViewAppoiments()       
    {
        dataBaseObj.sqlconn.Open();
        dataBaseObj.sqlAdt = new SqlDataAdapter("SELECT appID, appDateNow, appTimeNow, appReason, appDateofBooking, appTimeofBooking, clID, wuID FROM Appointments where appDateNow ='" +System.DateTime.Now.DayOfWeek+"'", dataBaseObj.sqlconn);
        dataBaseObj.dt = new DataTable("appoitment");
        dataBaseObj.sqlAdt.Fill(dataBaseObj.dt);
        dataBaseObj.sqlconn.Close();
        return dataBaseObj.dt;
    }
    // this method here is for the  therapist to view cuurent day appointment with oppoetunit to filter them 
    public DataTable ViewAllAppoiements(int id, string date)
    {
        // this method return the information of appointmenet where the id and date matchs since i have user the operatoe AND 
        dataBaseObj.sqlconn.Open();
        dataBaseObj.sqlAdt = new SqlDataAdapter("SELECT appID, appDateNow, appTimeNow, appReason, appDateofBooking, appTimeofBooking, clID, wuID FROM Appointments where appID ='" + id + "'AND where appDateNow='" + date +"'", dataBaseObj.sqlconn);
        dataBaseObj.dt = new DataTable("appoitment");
        dataBaseObj.sqlAdt.Fill(dataBaseObj.dt);
        dataBaseObj.sqlconn.Close();
        return dataBaseObj.dt;
    }
    // this method is to update appoitment by therapist
    public void UpdateAppoiment(int appID, string appDateNow, string appTimeNow, string appReason, string appDateofBooking, string appTimeofBooking, int clID, int wuID)
    {
        dataBaseObj.sqlconn.Open();
        appDateNow = Convert.ToString(System.DateTime.Now.Year + "/" + System.DateTime.Now.Month + "/" + System.DateTime.Now.DayOfWeek);

        appTimeNow = Convert.ToString(System.DateTime.Now.TimeOfDay);
        dataBaseObj.sqlCmd = new SqlCommand("UPDATE Appointments SET  appDateNow =@appDateNow, appTimeNow =@appTimeNow, appReason =@appReason, appDateofBooking =@appDateofBooking, appTimeofBooking =@appTimeofBooking, clID =@clID, wuID =@wuID where appID='" + appID + "'", dataBaseObj.sqlconn);



        dataBaseObj.sqlCmd.Parameters.AddWithValue("@appDateNow", appDateNow);
        dataBaseObj.sqlCmd.Parameters.AddWithValue("@appTimeNow", appTimeNow);

        dataBaseObj.sqlCmd.Parameters.AddWithValue("@appReason", appReason);
        dataBaseObj.sqlCmd.Parameters.AddWithValue("@appDateofBooking", appDateofBooking);

        dataBaseObj.sqlCmd.Parameters.AddWithValue("@appTimeofBooking", appTimeofBooking);
        dataBaseObj.sqlCmd.Parameters.AddWithValue("@clID", clID);
        dataBaseObj.sqlCmd.Parameters.AddWithValue("@wuID", wuID);
        dataBaseObj.sqlCmd.ExecuteNonQuery();
    }
    // this method is to delete the appoinmet
    public string deleteAppoinment(int appID)
    {
        dataBaseObj.sqlconn.Open();
        dataBaseObj.sqlCmd = new SqlCommand("DELETE FROM Appointments whre appID='" + appID + "'");
        dataBaseObj.sqlCmd.ExecuteNonQuery();
        intResult = intResult = dataBaseObj.sqlCmd.ExecuteNonQuery();
        if (intResult == 1)
        {
            strmessage = "registeed";
        }
        else
        {
            strmessage = "not registeed";
        }
        dataBaseObj.sqlconn.Close();
        return strmessage; 
    }
    // report order by date

    public DataTable listOfCLient()
    {
       dataBaseObj.sqlconn.Open();
        dataBaseObj.sqlAdt = new SqlDataAdapter("SELECT clID, clFirstName, clSurname, clStreetAddress, clCity, clPostCode, clHomePhone, clMobilePhone, clEmail, clPassword, clStatus FROM Client", dataBaseObj.sqlconn);
        dataBaseObj.dt = new DataTable("appoitment");
        dataBaseObj.sqlAdt.Fill(dataBaseObj.dt);
        dataBaseObj.sqlconn.Close();
        return dataBaseObj.dt;
    }
    public  DataTable listOfAppoint()
    {
        dataBaseObj.sqlconn.Open();
        dataBaseObj.sqlAdt = new SqlDataAdapter("SELECT        appID, appDateNow, appTimeNow, appReason, appDateofBooking, appTimeofBooking, clID, wuID FROM Appointments ORDER BY appDateNow", dataBaseObj.sqlconn);
        dataBaseObj.dt = new DataTable("appoitment");
        dataBaseObj.sqlAdt.Fill(dataBaseObj.dt);
        dataBaseObj.sqlconn.Close();
        return dataBaseObj.dt;
    }

    public DataTable listAppontBytherapy()
    {
        dataBaseObj.sqlconn.Open();
        dataBaseObj.sqlAdt = new SqlDataAdapter("SELECT  Appointments.appID, Appointments.appDateNow, Appointments.appTimeNow, Appointments.appReason, Appointments.appDateofBooking,Appointments.appTimeofBooking, Appointments.clID, Appointments.wuID, WorkUsers.wuUserType FROM Appointments INNER JOIN WorkUsers ON Appointments.wuID = WorkUsers.wuID ORDER BY WorkUsers.wuUserType", dataBaseObj.sqlconn);
        dataBaseObj.dt = new DataTable("appoitment");
        dataBaseObj.sqlAdt.Fill(dataBaseObj.dt);
        dataBaseObj.sqlconn.Close();
        return dataBaseObj.dt;
    }
}

}

...