public string AddHours(string datetime, long value) { date = new Datetime(); int day = date.GetDay(datetime); int month = date.GetMonth(datetime); int year = date.GetYear(datetime); int hour = date.GetHours(datetime); int minute = date.GetMinutes(datetime); //Variavel de saída string datimeOutup = string.Empty; while (value != 0) { date = new Datetime().Month(month, year); hour++; if (hour == 24) { //Mudou o dia day++; hour = 0; } if (day > date.LastDay) { //Mudou o mes day = 1; month++; hour = 0; } if (month == 13) { //Mudou o ano year++; month = 1; day = 1; hour = 0; } value--; if (value == 0) { return(string.Format("{0}/{1}/{2} {3}:{4}", day.ToString(), month.ToString(), year.ToString(), hour.ToString(), minute.ToString())); } } return(datetime); }
public string Format(string datetime, int format_type) { date = new Datetime(); //Obter dia usando método da classe int day = date.GetDay(datetime); //Obter mês usando método da classe int month = date.GetMonth(datetime); //Obter ano usando método da classe int year = date.GetYear(datetime); //Obter hora usando método da classe int hour = date.GetHours(datetime); //Obter minutos usando método da classe int minutes = date.GetMinutes(datetime); //Testes usando ternários para determinar a formatação da string de retorno. //Dia e mês string dayStr = day < 10 ? "0" + day.ToString() : day.ToString(); string monthStr = month < 10 ? "0" + month.ToString() : month.ToString(); //Hora e minuto string hoursStr = hour < 10 ? "0" + hour.ToString() : hour.ToString(); string minutesStr = minutes < 10 ? "0" + minutes.ToString() : minutes.ToString(); string monthName = date.Month(month, year).Name; //Formatar a data. if (format_type == 2) { //2 -> dd-mm-yyyy hh:mm return(string.Format("{0}-{1}-{2} {3}:{4}", dayStr, monthStr, year.ToString(), hoursStr, minutesStr).ToUpper()); } if (format_type == 3) { //3 -> dd-Mes-yyyy hh:mm return(string.Format("{0}-{1}-{2} {3}:{4}", dayStr, monthName, year.ToString(), hoursStr, minutesStr).ToUpper()); } if (format_type == 4) { // 4 -> dd de Mes de yyyy, hh hora(s) e mm minuto(s)"); return(string.Format("{0} de {1} de {2}, {3} hora(s) e {4} minuto(s)", dayStr, monthName, year.ToString(), hoursStr, minutesStr).ToUpper()); } //1 -> dd/mm/yyyy hh:mm //Tipo 1 é formato default return(string.Format("{0}/{1}/{2} {3}:{4}", dayStr, monthStr, year.ToString(), hoursStr, minutesStr)); }
public string SubtractMinutes(string datetime, long value) { //Subtract = 4000 (minutes) //Input = "04/03/2010 17:40" //Output = "01/03/2010 23:00" Datetime date = new Datetime(); int day = date.GetDay(datetime); int month = date.GetMonth(datetime); int year = date.GetYear(datetime); int hour = date.GetHours(datetime); int minute = date.GetMinutes(datetime); //Variavel de saída string datimeOutup = string.Empty; while (value != 0) { date = new Datetime().Month(month, year); minute--; if (minute < 0) { //último minuto minute = 59; if (hour == 0) { //última hora hour = 23; } else { //Subtrair hora hour--; } } if (hour == 23 && minute == 59) { //Mudou o dia day--; } if (day == 0) { //Mudou o mês month--; if (day == 0) { //Pegar último dia do mês day = date.LastDay; } } if (month == 0) { //Último dia do ano year--; month = 12; day = date.LastDay; } value--; if (value == 0) { return(datimeOutup = string.Format("{0}/{1}/{2} {3}:{4}", day.ToString(), month.ToString(), year.ToString(), hour.ToString(), minute.ToString())); } } //Retornando string vazia caso nao existe incremento return(datimeOutup); }
public string AddMinutes(string datetime, long value) { //Add = 4000 (minutes) //Input = "01/03/2010 23:00" //Output = "04/03/2010 17:40" date = new Datetime(); int day = date.GetDay(datetime); int month = date.GetMonth(datetime); int year = date.GetYear(datetime); int hour = date.GetHours(datetime); int minute = date.GetMinutes(datetime); //Variavel de saída string datimeOutup = datetime; while (value != 0) { date = new Datetime().Month(month, year); minute++; if (minute == 60) { //Mudou a hora hour++; minute = 0; } if (hour == 24) { //Mudou o dia day++; hour = 0; minute = 0; } if (day > date.LastDay) { //Mudou o mes day = 1; month++; hour = 0; minute = 0; } if (month == 13) { //Mudou o ano year++; month = 1; day = 1; hour = 0; minute = 0; } value--; if (value == 0) { return(string.Format("{0}/{1}/{2} {3}:{4}", day.ToString(), month.ToString(), year.ToString(), hour.ToString(), minute.ToString())); } } return(datimeOutup); }