calculate the number of days between two dates

mta

Client
Регистрация
12.01.2020
Сообщения
129
Благодарностей
7
Баллы
18
In Zennoposter , i need to calculate number of days between 2 dates and put the result in a variable. Is there anyway to do it in ZP without coding? If not can someone help me by a snipet, i 'am not a coder :(
 

Phoenix78

Client
Read only
Регистрация
06.11.2018
Сообщения
11 790
Благодарностей
5 690
Баллы
113
C#:
DateTime date1 = DateTime.ParseExact("2020-10-16 2-06-39--325", "yyyy-MM-dd H-mm-ss--fff", CultureInfo.InvariantCulture);
DateTime date2 = DateTime.ParseExact("2020-10-14 2-06-39--325", "yyyy-MM-dd H-mm-ss--fff", CultureInfo.InvariantCulture);

int days = (date1 - date2).Days;
return days;
65640
 
  • Спасибо
Реакции: mta

mta

Client
Регистрация
12.01.2020
Сообщения
129
Благодарностей
7
Баллы
18
Great! thanks! But i used this DateTime.Parse for counting Days only.

DateTime date1 = DateTime.Parse(project.Variables["v1"].Value);
DateTime date2 = DateTime.Parse(project.Variables["v2"].Value);

int days = (date2 - date1).Days;
return days;
Now i 'am wondering how to manage date format. My date format stored in variable is dd/MM/YYYY and environnemental variable of zennopster is MM.dd.YYYY. Some Tips ?
 

nicanil

Client
Регистрация
06.03.2016
Сообщения
2 242
Благодарностей
1 804
Баллы
113
On this page you can find available format specifiers and their description.

Code for formats described in your message:
C#:
DateTime mta_date = DateTime.ParseExact("02/03/2020", "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture);
DateTime zp_date = DateTime.ParseExact("03.09.2020", "MM.dd.yyyy", System.Globalization.CultureInfo.InvariantCulture);

int days = (mta_date - zp_date).Days;
return days;
 
  • Спасибо
Реакции: mta

mta

Client
Регистрация
12.01.2020
Сообщения
129
Благодарностей
7
Баллы
18
On this page you can find available format specifiers and their description.

Code for formats described in your message:
C#:
DateTime mta_date = DateTime.ParseExact("02/03/2020", "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture);
DateTime zp_date = DateTime.ParseExact("03.09.2020", "MM.dd.yyyy", System.Globalization.CultureInfo.InvariantCulture);

int days = (mta_date - zp_date).Days;
return days;
Great !!! Thanks
 

mta

Client
Регистрация
12.01.2020
Сообщения
129
Благодарностей
7
Баллы
18
hello !

I get an error while converting date in good format:

Compiling code of Error in action "CS1503" "Argument 3: cannot convert from 'System.Globalization.CultureInfo' to 'System.Globalization.DateTimeStyles'". [Row: 13; Column: 91]

Get number of days between 2 dates:
System.Globalization.CultureInfo culture = System.Globalization.CultureInfo.CreateSpecificCulture("fr-FR");
DateTime date1 = DateTime.Parse(project.Variables["v1"].Value, "dd/MM/yyyy", culture);
DateTime date2 = DateTime.Parse(project.Variables["v2"].Value, "dd/MM/yyyy", culture);

int days = (date2 - date1).Days;

return days;


:bc::bc::bc::bc::bc::bc::bc::bc::bc::bc::bc::bc::bc::bc::bc:
 

nicanil

Client
Регистрация
06.03.2016
Сообщения
2 242
Благодарностей
1 804
Баллы
113
hello !

I get an error while converting date in good format:

Compiling code of Error in action "CS1503" "Argument 3: cannot convert from 'System.Globalization.CultureInfo' to 'System.Globalization.DateTimeStyles'". [Row: 13; Column: 91]

Get number of days between 2 dates:
System.Globalization.CultureInfo culture = System.Globalization.CultureInfo.CreateSpecificCulture("fr-FR");
DateTime date1 = DateTime.Parse(project.Variables["v1"].Value, "dd/MM/yyyy", culture);
DateTime date2 = DateTime.Parse(project.Variables["v2"].Value, "dd/MM/yyyy", culture);

int days = (date2 - date1).Days;

return days;


:bc::bc::bc::bc::bc::bc::bc::bc::bc::bc::bc::bc::bc::bc::bc:
Because you use DateTime.Parse and this method doesn't take CultureInfo as the third argument. You should use DateTime.ParseExact instead.

C#:
System.Globalization.CultureInfo culture = System.Globalization.CultureInfo.CreateSpecificCulture("fr-FR");
DateTime date1 = DateTime.ParseExact(project.Variables["v1"].Value, "dd/MM/yyyy", culture);
DateTime date2 = DateTime.ParseExact(project.Variables["v2"].Value, "dd/MM/yyyy", culture);

int days = (date2 - date1).Days;

return days;
 
  • Спасибо
Реакции: mta

mta

Client
Регистрация
12.01.2020
Сообщения
129
Благодарностей
7
Баллы
18
Because you use DateTime.Parse and this method doesn't take CultureInfo as the third argument. You should use DateTime.ParseExact instead.

C#:
System.Globalization.CultureInfo culture = System.Globalization.CultureInfo.CreateSpecificCulture("fr-FR");
DateTime date1 = DateTime.ParseExact(project.Variables["v1"].Value, "dd/MM/yyyy", culture);
DateTime date2 = DateTime.ParseExact(project.Variables["v2"].Value, "dd/MM/yyyy", culture);

int days = (date2 - date1).Days;

return days;

Great, thanks i will never be able to handle this mistake alone ! :ay::ay::ay::ay::ay::ay::ay::ay::ay::ay:
 
  • Спасибо
Реакции: nicanil

Кто просматривает тему: (Всего: 1, Пользователи: 0, Гости: 1)