Formatting a string - asp.net

I have got a string in the form:
8 AM;10:15 AM;3:30 PM;6:15 PM
which i need to format in the form
8 AM-10:15 AM, 3:30 PM-6:15 PM
How can i achieve this?

You could do string splitting on ;, like this
var parts = s.Split(";".ToCharArray());
Then you could use the parts, like this:
var result = string.Format("{0}-{1}, {2}-{3}", parts);
This requires that the string always contains 4 parts.

Related

How do I show time in ASP.NET?

I have a label in my asp.net web site that will shows the time. I want the output like here. in the morning like this: 08:26 and after 12 am,it shows 15:28
My code does not work. It only supports the first part.
DateTime tim = DateTime.Now;
int hh = p.GetHour(tim);
int mm = p.GetMinute(tim);
Label7.Text = DateTime.Now.ToString("hh:mm");
According to the Custom date and time format strings docs page - you can see:
"hh" The hour, using a 12-hour clock from 01 to 12.
"HH" The hour, using a 24-hour clock from 00 to 23.
So in your case - just use the capitalized HH for your formatting:
Label7.Text = DateTime.Now.ToString("HH:mm");
and you should get what you're looking for.

LocalDate pattern that shows day as weekday

I search for a pattern which looks like this: Thu, 11. 05. 1999
So I tried this syntax:
String pattern = "DDD, dd. mm. uuuu";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
But it won't work. When trying to format a LocalDate I get java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: MinuteOfHour.
A look into the official documentation of Java explaining the format pattern syntax is always good:
E = day-of-week
M = month
m = minute
So you should try this pattern:
EEE, dd. MM. uuuu
And setting the locale explicitly to English is also a good idea.

correct sum of hours in access

I have two columns in an access 2010 database with some calculated field:
time_from time_until calculated_field(time_until-time_from)
10:45 15:00 4:15
13:15 16:00 2:45
11:10 16:00 4:50
08:00 15:00 7:00
08:00 23:00 15:00
Now so far, it is good: calculated field did its job to tell me total hours and mins...
now, I need a sum of a calculated field....
I put in an expression builder: =Sum([time_until]-[time_from])
I guess total sum should give me 33:50... but it gives me some 9:50. why is this happening? Is there a way to fix this?
update:
when I put like this:
=Format(Sum([vrijeme_do]-[vrijeme_od])*24)
I get a decimal point number... which I suppose is correct....
for example, 25hrs and 30mins is shown as 25,5
but, how do I format this 25,5 to look like 25:30?
As #Arvo mentioned in his comment, this is a formatting problem. Your expected result for the sum of calculated_field is 33:50. However that sum is a Date/Time value, and since the number of hours is greater than 24, the day portion of the Date/Time is advanced by 1 and the remainder 9:50 is displayed as the time. Apparently your total is formatted to display only the time portion; the day portion is not displayed.
But the actual Date/Time value for the sum of calculated_field is #12/31/1899 09:50#. You can use a custom function to display that value in your desired format:
? duration_hhnn(#12/31/1899 09:50#)
33:50
This is the function:
Public Function duration_hhnn(ByVal pInput As Date) As String
Dim lngDays As Long
Dim lngMinutes As Long
Dim lngHours As Long
Dim strReturn As String
lngDays = Int(pInput)
lngHours = Hour(pInput)
lngMinutes = Minute(pInput)
lngHours = lngHours + (lngDays * 24)
strReturn = lngHours & ":" & Format(lngMinutes, "00")
duration_hhnn = strReturn
End Function
Note the function returns a string value so you can't do further date arithmetic on it directly.
Similar to the answer from #HansUp, it can be done without VBA code like so
Format(24 * Int(SUM(elapsed_time)) + Hour(SUM(elapsed_time)), "0") & ":" & Format(SUM(elapsed_time), "Nn")
I guess you are trying to show the total in a text box? the correct expression would be =SUM([calculated_field_name]).

check if a string's first 16 char is a number

I'm storing some files in database which has filename like 1839341255115211butterflies.jpg.I need to show this filename to the user as butterflies.jpg.I need to remove the first 16 digit and then show the filename.Added to it I also have few filenames which don't have this 16digit addition prior to the filename.Now my question is how do I identify if this string has 16digit numeric value prior to the filename, based on it remove the 1st 16digit and display just the filename. I'm aware of how to remove the first 16digit and retrive the filename but need help on how to identify a string that has 16digit.
Any suggestion is much appreciated.
A regular expression looks like a good fit here:
^[0-9]{16}
The above will match on strings that start with 16 digits (0 to 9).
Usage:
if(Regex.Match(fileName, #"^[0-9]{16}").Success)
{
fileName = fileName.Remove(0, 16);
}
string.Remove will work quite nicely:
var str = "1839341255115211butterflies.jpg";
str = str.Remove(0, 16);
Console.WriteLine(str);
With Linq:
remove all digits at the beginning until 16 digits:
string file = "1839341255115211butterflies.jpg";
string extension = Path.GetExtension(file);
string fileName = Path.GetFileNameWithoutExtension(file);
fileName = new string(fileName.Where((c, i) => i >= 17 || !Char.IsDigit(c)).ToArray());
file = fileName + extension;
Demo
Edit: If you just want to know if the first 16 chars are digits, it's easier and more readable:
bool startsWith16Digits = file.Take(16).All(Char.IsDigit);

How to write custom regular expression

I Would like to write a custom regular expression where the format is like XYZ-ABC-001
where
XYZ is fixed,
ABC is variable (it can be any three characters between A-Z )
001 is variable (it can be any number between 0-9)
This will match:
XYZ-[a-zA-Z]{3}-\d+
DIY kit: http://txt2re.com/
Example: http://txt2re.com/index-csharp.php3?s=XYZ-ABC-001&4&22&23&24&-17&-18&14&15&16
Results in the C# output:
string txt="XYZ-ABC-001";
string re1="((?:[a-z][a-z]+))"; // Word 1
string re2="(-)"; // Any Single Character 1
string re3="(.)"; // Any Single Character 2
string re4="(.)"; // Any Single Character 3
string re5="(.)"; // Any Single Character 4
string re6="(-)"; // Any Single Character 5
string re7="(\\d)"; // Any Single Digit 1
string re8="(\\d)"; // Any Single Digit 2
string re9="(\\d)"; // Any Single Digit 3
Regex r = new Regex(re1+re2+re3+re4+re5+re6+re7+re8+re9,RegexOptions.IgnoreCase|RegexOptions.Singleline);
Match m = r.Match(txt);
if (m.Success)
{
String word1=m.Groups[1].ToString();
String c1=m.Groups[2].ToString();
String c2=m.Groups[3].ToString();
String c3=m.Groups[4].ToString();
String c4=m.Groups[5].ToString();
String c5=m.Groups[6].ToString();
String d1=m.Groups[7].ToString();
String d2=m.Groups[8].ToString();
String d3=m.Groups[9].ToString();
Console.Write("("+word1.ToString()+")"+"("+c1.ToString()+")"+"("+c2.ToString()+")"+"("+c3.ToString()+")"+"("+c4.ToString()+")"+"("+c5.ToString()+")"+"("+d1.ToString()+")"+"("+d2.ToString()+")"+"("+d3.ToString()+")"+"\n");
}
Then you can modify it slightly to adjust to your needs, for example replacing (.) with ([A-Z]).

Resources