locked
Custom Functions RRS feed

  • Question

  • User-797751191 posted

    Hi

      How we can create our own custom functions for day & Format

    Thanks

    Saturday, August 10, 2019 9:43 AM

All replies

  • User753101303 posted

    Hi,

    Some more context could help. Do you mean you don't have for date formatting, any culture that fit your needs ? You can create a custom culture https://docs.microsoft.com/en-us/dotnet/api/system.globalization.cultureandregioninfobuilder?view=netframework-4.8 or  a custom https://docs.microsoft.com/en-us/dotnet/api/system.iformatprovider?view=netframework-4.8 but for now I believe you perhaps not explored yet what is available out of the box or you lack direction about handling this ?

    Saturday, August 10, 2019 9:58 AM
  • User-797751191 posted

    Hi Patrice

      Can u pls suggest on to find day,month,year using own custom function . 

    Thanks

    Saturday, August 10, 2019 10:20 AM
  • User475983607 posted

    Can u pls suggest on to find day,month,year using own custom function . 

    Your question is too vague to answer.

    See the official DateTime format functions if you need to display a date in a specific format.

    https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-date-and-time-format-strings

    https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings

    The DateTime structure has methods and properties that return the day, month, and year.  Again, please try reading the official documentation.

    https://docs.microsoft.com/en-us/dotnet/api/system.datetime?view=netframework-4.8

    If you want a forum member to review your code then you'll need to share the custom function.  Explain the expected results and the actual results.

    Saturday, August 10, 2019 10:50 AM
  • User753101303 posted

    I still don't get what you mean. If you have  a valid DateTime just look at its properties ie https://docs.microsoft.com/en-us/dotnet/api/system.datetime?view=netframework-4.8#properties (Day, Month and Year properties). Why would you need custom functions ?

    If you need to convert a string to a DateTime it can depend on the current "culture" you could define in the web.config file using https://documentation.devexpress.com/AspNet/12062/Localization/How-to-Set-a-Culture-for-an-ASP-NET-Web-Page

    Keep in mind we have no other information than what you explicitely tell us to understand what you are trying to do. For now it seems something you can easily do out of the box.

    Or finally if you tried something and it fails, it could be easier to tell which problem you are trying to solve...

    Saturday, August 10, 2019 12:27 PM
  • User-797751191 posted

    Hi PatricSc

      Like below which i have created for left . Can we see the code of existing Day function

    Public Function myLeft(ByVal sourceStr As String, ByVal noOfChars As Integer) As String
    If sourceStr.Length = 0 Or noOfChars < 0 Then Return ""
    If sourceStr.Length <= noOfChars Then Return sourceStr
    sourceStr = Microsoft.VisualBasic.Left(sourceStr, noOfChars)
    Return sourceStr
    End Function

    Thanks

    Saturday, August 10, 2019 2:00 PM
  • User753101303 posted

    It's best to work on "native data" as much as possible ie a date should be converted to a string just before shown to users and the string input should be converted to a date before further processing. Even with web forms you could use something such as https://docs.microsoft.com/en-us/aspnet/web-forms/overview/presenting-and-managing-data/model-binding/retrieving-data the data to string or string to data conversion being handled for you based on the current culture and specific information you can  add on properties.

    You have a form with an date input field ? You are using a datepicker or a text input field ? Your site supports a single "language" (which one ?)

    Saturday, August 10, 2019 6:34 PM
  • User288213138 posted

    Hi jsshivalik,

    Do you mean you want to create a function that converts dates?

    If so, you can try below code(base on DataTime source code):

    The first parameter is the string to determine whether its length is 0 and the value is null.

    The second parameter is culture-specific information about the format of date and time values.

    The third parameter define the formatting options that customize string parsing for some date and time parsing methods.

    The fourth parameter is the result returned.

    internal unsafe static bool TryParse(string s, DateTimeFormatInfo dtfi, DateTimeStyles styles, ref DateTimeResult result)
    {
    	if (s == null)
    	{
    		result.SetFailure(ParseFailureKind.ArgumentNull, "ArgumentNull_String", null, "s");
    		return false;
    	}
    	if (s.Length == 0)
    	{
    		result.SetFailure(ParseFailureKind.Format, "Format_BadDateTime", null);
    		return false;
    	}
    	DS dS = DS.BEGIN;
    	bool flag = false;
    	DateTimeToken dtok = default(DateTimeToken);
    	dtok.suffix = TokenType.SEP_Unk;
    	DateTimeRawInfo raw = default(DateTimeRawInfo);
    	int* numberBuffer = stackalloc int[3];
    	raw.Init(numberBuffer);
    	raw.hasSameDateAndTimeSeparators = dtfi.DateSeparator.Equals(dtfi.TimeSeparator, StringComparison.Ordinal);
    	result.calendar = dtfi.Calendar;
    	result.era = 0;
    	__DTString str = new __DTString(s, dtfi);
    	str.GetNext();
    	do
    	{
    		if (!Lex(dS, ref str, ref dtok, ref raw, ref result, ref dtfi, styles))
    		{
    			return false;
    		}
    		if (dtok.dtt == DTT.Unk)
    		{
    			continue;
    		}
    		if (dtok.suffix != TokenType.SEP_Unk)
    		{
    			if (!ProcessDateTimeSuffix(ref result, ref raw, ref dtok))
    			{
    				result.SetFailure(ParseFailureKind.Format, "Format_BadDateTime", null);
    				return false;
    			}
    			dtok.suffix = TokenType.SEP_Unk;
    		}
    		if (dtok.dtt == DTT.NumLocalTimeMark)
    		{
    			if (dS == DS.D_YNd || dS == DS.D_YN)
    			{
    				return ParseISO8601(ref raw, ref str, styles, ref result);
    			}
    			result.SetFailure(ParseFailureKind.Format, "Format_BadDateTime", null);
    			return false;
    		}
    		if (raw.hasSameDateAndTimeSeparators)
    		{
    			if (dtok.dtt == DTT.YearEnd || dtok.dtt == DTT.YearSpace || dtok.dtt == DTT.YearDateSep)
    			{
    				if (dS == DS.T_Nt)
    				{
    					dS = DS.D_Nd;
    				}
    				if (dS == DS.T_NNt)
    				{
    					dS = DS.D_NNd;
    				}
    			}
    			bool flag2 = str.AtEnd();
    			if ((dateParsingStates[(int)dS][(int)dtok.dtt] == DS.ERROR) | flag2)
    			{
    				switch (dtok.dtt)
    				{
    				case DTT.YearDateSep:
    					dtok.dtt = (flag2 ? DTT.YearEnd : DTT.YearSpace);
    					break;
    				case DTT.NumDatesep:
    					dtok.dtt = (flag2 ? DTT.NumEnd : DTT.NumSpace);
    					break;
    				case DTT.NumTimesep:
    					dtok.dtt = (flag2 ? DTT.NumEnd : DTT.NumSpace);
    					break;
    				case DTT.MonthDatesep:
    					dtok.dtt = (flag2 ? DTT.MonthEnd : DTT.MonthSpace);
    					break;
    				}
    			}
    		}
    		dS = dateParsingStates[(int)dS][(int)dtok.dtt];
    		if (dS == DS.ERROR)
    		{
    			result.SetFailure(ParseFailureKind.Format, "Format_BadDateTime", null);
    			return false;
    		}
    		if (dS <= DS.ERROR)
    		{
    			continue;
    		}
    		if ((dtfi.FormatFlags & DateTimeFormatFlags.UseHebrewRule) != 0)
    		{
    			if (!ProcessHebrewTerminalState(dS, ref result, ref styles, ref raw, dtfi))
    			{
    				return false;
    			}
    		}
    		else if (!ProcessTerminaltState(dS, ref result, ref styles, ref raw, dtfi))
    		{
    			return false;
    		}
    		flag = true;
    		dS = DS.BEGIN;
    	}
    	while (dtok.dtt != 0 && dtok.dtt != DTT.NumEnd && dtok.dtt != DTT.MonthEnd);
    	if (!flag)
    	{
    		result.SetFailure(ParseFailureKind.Format, "Format_BadDateTime", null);
    		return false;
    	}
    	AdjustTimeMark(dtfi, ref raw);
    	if (!AdjustHour(ref result.Hour, raw.timeMark))
    	{
    		result.SetFailure(ParseFailureKind.Format, "Format_BadDateTime", null);
    		return false;
    	}
    	bool bTimeOnly = result.Year == -1 && result.Month == -1 && result.Day == -1;
    	if (!CheckDefaultDateTime(ref result, ref result.calendar, styles))
    	{
    		return false;
    	}
    	if (!result.calendar.TryToDateTime(result.Year, result.Month, result.Day, result.Hour, result.Minute, result.Second, 0, result.era, out DateTime result2))
    	{
    		result.SetFailure(ParseFailureKind.FormatBadDateTimeCalendar, "Format_BadDateTimeCalendar", null);
    		return false;
    	}
    	if (raw.fraction > 0.0)
    	{
    		result2 = result2.AddTicks((long)Math.Round(raw.fraction * 10000000.0));
    	}
    	if (raw.dayOfWeek != -1 && raw.dayOfWeek != (int)result.calendar.GetDayOfWeek(result2))
    	{
    		result.SetFailure(ParseFailureKind.Format, "Format_BadDayOfWeek", null);
    		return false;
    	}
    	result.parsedDate = result2;
    	if (!DetermineTimeZoneAdjustments(ref result, styles, bTimeOnly))
    	{
    		return false;
    	}
    	return true;
    }
    

    More information about tryparse() method you can use ilspy tool to view the source code.

    Best Regard,
    Sam

    Monday, August 12, 2019 8:31 AM