Date to unix time problem

Trying to translate the date i.e. ‘[5/18/2022 , 3/15/2022 , ]’ to unixtime using function DateToUnixTime, but there is error when debugging.

“[Executing block unix] FormatException: String ‘[5/18/2022 , 3/15/2022 , ]’ was not recognized as a valid DateTime.”

How to modify it?
thanks

Did you write the date format? It cannot recognize it automatically, you have to manually specify the format. There should be some info or a link in the block info section.

Hi @Ruri , thanks my bro
I wrote the date format, m/dd/yyyy, in the block, but the error still existed.
Actually, the date list has 3 string values, the 3rd is empty, I wonder if the 3rd empty string causes the error.
I do not know how to modify it.

by the way OB1 could deal with it automatically

Oh I just realized it’s a list. You need to convert them one by one, I made a guide on how to execute a block function for each item of a list

It’s the 2nd post

1 Like

I actually tried that long ago btw

(16,11): error CS0029: Cannot implicitly convert type 'int' to 'string'

This is the error it gives :point_up:

try this

BLOCK:ConstantList

  value = ["5/18/2022", "3/15/2022"]

  => VAR @list

ENDBLOCK

for (int i = 0; i < list.Count; i++)

{

BLOCK:DateToUnixTime

  datetime = @list[i]

  format = "M/dd/yyyy"

  => VAR @unix

ENDBLOCK

  list[i] = unix;

}

I have tried many times, but does not work
Parsed a variable, called expiredtime, with its values [ “5/18/2022”, “3/15/2022”,""]
How to use this list for if loop? is it expiredtime(i)
thanks @Ruri

BLOCK:ConstantList
  value = ["5/18/2022", "3/15/2022"]
  => VAR @list
ENDBLOCK

for (int i = 0; i < list.Count; i++)
{
BLOCK:DateToUnixTime
  datetime = @list[i]
  format = "M/dd/yyyy"
  => VAR @unix
ENDBLOCK

  list[i] = unix.AsString();
}

Thanks @Ruri

I tried but it frozen on the empty value of datetime in the parsed list [ “5/18/2022”, “3/15/2022”,""].

[Executing block DateToUnixTime] FormatException: String ’ ’ was not recognized as a valid DateTime.

Well if it’s empty then of course it will fail converting it. What should happen for an empty value? Should it output 0? Should it skip it?

Thanks @Ruri
They came from the parse, no 0 output, nothing, like this, [""]
OB1 can skip it but OB2 could not, there is an error, there.
do not know why.

BLOCK:ConstantList
  value = ["5/18/2022", "3/15/2022"]
  => VAR @list
ENDBLOCK

for (int i = 0; i < list.Count; i++)
{
    if (list[i] == "") continue;

BLOCK:DateToUnixTime
  datetime = @list[i]
  format = "M/dd/yyyy"
  => VAR @unix
ENDBLOCK

  list[i] = unix.AsString();
}

I tried this script, but it is not working yet.
The error:
[Executing block DateToUnixTime] FormatException: String ’ ’ was not recognized as a valid DateTime.

The script of Ruri is working bro
11
‘[5/18/2022 , 3/15/2022 , ]’
Try to first start removing the characters that bother you to avoid falling into an exception

Two soluce for your problem :


data.ExecutingBlock("Problem Date");

string Date = ConstantString(data, "[5/18/2022 , 3/15/2022 , ]");


// BLOCK: Trim

data.ExecutingBlock("Trim");

string trimOutput = Trim(data, $"{Date}");

// BLOCK: Erase

data.ExecutingBlock("Erase");

string parsing_One = ParseBetweenStrings(data, $"{trimOutput}", "[", "]", true, "", "", false);

// BLOCK: First Date 

data.ExecutingBlock("First Date ");

string parsing_Two = ParseBetweenStrings(data, $"{parsing_One}", "", ",", true, "", "", false);

// BLOCK: SecondDate

data.ExecutingBlock("SecondDate");

string parsing_Three = ParseBetweenStrings(data, $"{parsing_One}", ",", ",", true, "", "", false);

// BLOCK: Date To Unix Time One

data.ExecutingBlock("Date To Unix Time One");

int FirstDate = DateToUnixTime(data, parsing_Two.AsString(), "M/dd/yyyy");

// BLOCK: Date To Unix Time Two

data.ExecutingBlock("Date To Unix Time Two");

int SecondDate = DateToUnixTime(data, parsing_Three.AsString(), "M/dd/yyyy");```


--------------------------------------------------------------------------------------------------------------------------------------------------------
Second Soluce : 
// BLOCK: Problem Date
data.ExecutingBlock("Problem Date");
string Date = ConstantString(data, "[5/18/2022 , 3/15/2022 , ]");

// BLOCK: First
data.ExecutingBlock("First");
string replaceOutput = Replace(data, Date.AsString(), "[", "");

// BLOCK: Two
data.ExecutingBlock("Two");
string Two = Replace(data, $"{replaceOutput}", "]", "");

// BLOCK: Three
data.ExecutingBlock("Three");
string Three = Replace(data, Two.AsString(), ",", "");

// BLOCK: INTO
data.ExecutingBlock("INTO");
string parseOutput = ParseBetweenStrings(data, $"{Three}", "", "", true, "", "", false);

// BLOCK: FirstDate
data.ExecutingBlock("FirstDate");
string DateOne = Substring(data, $"{parseOutput}", 0, 9);

// BLOCK: SecondDate
data.ExecutingBlock("SecondDate");
string DateTwo = Substring(data, $"{parseOutput}", 9, 11);

// BLOCK: End 1
data.ExecutingBlock("End 1");
int dateToUnixTimeOutput = DateToUnixTime(data, DateOne.AsString(), "M/dd/yyyy");

// BLOCK: End 2
data.ExecutingBlock("End 2");
dateToUnixTimeOutput = DateToUnixTime(data, DateTwo.AsString(), "M/dd/yyyy");

--------------------------------------------------------------------------------------------------------------------------------------------------------

You must not copy the code, you must understand it and above all adapt it to your program I tried as much as possible to reproduce your problem and took the same value as you obtain in constant for the tutorial