-2

I'm having a readonly text box and a button in C#. When I'm clicking on button it should display in a text box in below manner:

MYYMMJJSS.

M: Manual, YY: The last digits of year, MM: The month, JJ: The Day, SS: The Sequence for the concerned Day

Examples: • On 1st February 2019, a first order is created by user. The system proposes this value M19020101.

• On 1st February 2019, a second manual order is created by user. The system proposes this value M19020102.

Can anyone helps me to understand how can i add a M (manual) and Sequence number in the dates?

Deepak
  • 614
  • 1
  • 7
  • 20
Naman
  • 37
  • 1
  • 10
  • What you tried so far? We are here to help not to work for you. What part is a problem? Where are you struggling? Post a [MCVE] even with pseudo code. – aloisdg Jul 25 '18 at 04:44
  • Tried reading this ? https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings – Chetan Jul 25 '18 at 04:47
  • Try this, it is similar to what you need https://stackoverflow.com/questions/9601593/how-can-i-format-07-03-2012-to-march-7th-2012-in-c-sharp – Pranav Singh Jul 25 '18 at 04:49
  • I tried: private void NumberValidationTxtNFab() { int index = 1; var dates = DateTime.Now.ToString("yyMMdd"); if (!String.IsNullOrEmpty(dates)) { dates = "M" + dates + "0" + index; index++; } var _txtNFab = txtNFab.Text; _txtNFab = dates; } – Naman Jul 25 '18 at 05:01
  • 1
    Possible duplicate of [How can i format 07/03/2012 to March 7th,2012 in c#](https://stackoverflow.com/questions/9601593/how-can-i-format-07-03-2012-to-march-7th-2012-in-c-sharp) – Albert Einstein Jul 25 '18 at 05:37

3 Answers3

0
    static void Main(string[] args)
    {
        int sequenceNumber = 12;
        string theString = string.Format("M{0}{1}", DateTime.Now.ToString("yyMMdd"), sequenceNumber);
        Console.WriteLine(theString);
        Console.ReadLine();
    }
Pieter Alberts
  • 829
  • 1
  • 7
  • 23
0

You can use this way:

int sequenceNumber = 1;
string text = $"M{DateTime.Now:yyMMdd}{sequenceNumber:D2}";
Console.WriteLine(text);
  • yyMMdd: format datetime to string
  • D2: format integer so it contain at least 2 digits

Based on your new comment, you can rewrite your code something similar to:

int index = 1;

private void NumberValidationTxtNFab()
{
    txtNFab.Text = $"M{DateTime.Now:yyMMdd}{index:D2}";
    index++;
}
NoName
  • 7,940
  • 13
  • 56
  • 108
  • private void NumberValidationTxtNFab() { int index = 1; var dates = DateTime.Now.ToString("yyMMdd"); if (!String.IsNullOrEmpty(dates)) { dates = "M" + dates + "0" + index; index++; } var _txtNFab = txtNFab.Text; _txtNFab = dates; } – Naman Jul 25 '18 at 05:00
  • @Naman if index is 1, your code will output `M18072501` (9 chars), but if index is 11, it will output `M180725011` (10 chars), meanwhile my code will output `M18072511` (still 9 chars) – NoName Jul 25 '18 at 05:03
  • its working for first time, M18072501 but second time if i click it is showing same "M18072501" instead of "M18072502". – Naman Jul 25 '18 at 05:12
  • @Naman, you must put the `index` variable outside the function. You can see my edit – NoName Jul 25 '18 at 05:13
0

The DateTime.ToString() supports escaping literals. See here: https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings#character-literals and here: https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings#escape

But for the sequence number you need either legacy or new way of interpolation as suggested by others, or concatenation.

ZorgoZ
  • 2,974
  • 1
  • 12
  • 34