0

I have email subjects stored in DB like this "Sample Result {MM/dd/yyyy}" or "Sample {MM/dd/yyyy} Result". I need to get the string and replace the date format with current date. The date formats in the string might vary for example it can be "{dd-MM-yyyy}" also. If the data in DB was like "Sample Result {0:MM/dd/yyyy}" I can use the below code:

string.Format(formattedSubject, DateTime.Now)

But how to make it work with "Sample Result {MM/dd/yyyy}" I know I can use:

string formattedSubject = subject.Replace("{","{0:");

But I think that's not the ideal solution. Any other ideas?

bit500
  • 27
  • 1
  • 4
  • Maybe `subject.Replace("{MM/dd/yyyy}", DateTime.Now.ToString("MM/dd/yyyy"))`? – vernou Aug 04 '21 at 15:09
  • The date format can be different. That's why I cannot just use a replace. The date format can be MM-dd-yy also – bit500 Aug 04 '21 at 15:10
  • If you few formats, you can chain the replace. Else a regex can do the job. – vernou Aug 04 '21 at 15:13
  • It can be any date format, so not sure how to use regex in that case. – bit500 Aug 04 '21 at 15:23
  • 3
    I am guessing you need to re-think having “different” date formats in the same file. You state that the date could be “MM/dd/yyyy” or “dd-MM-yyyy” …. This IS a problem for you. If you get a date like “06/04/2021” … which format is this? MM/dd/yyyy or dd/MM/yyyy? It is never a good idea to MIX date formats. The problem is that when your code misinterprets the date… YOUR code will be blamed… a consistent format is the only way to guarantee your code interprets the correct date. – JohnG Aug 04 '21 at 15:26
  • `I have email subjects stored in DB` and `I need to get the string and replace the date format with current date`, this is an operation that can be done in the DB more than likely, but your post is lacking information about this. On another note, how are these records getting stored, why can't they include the current date when stored, are you updating existing records that are wrong? – Trevor Aug 04 '21 at 15:28
  • Just to add clarity, each client will have different email subject headers and the date format they want. So in DB for one client it might "Sample Whatever {dd-MM-YYYY}" . So while sending an email I just get the email subject format from DB for that client and replace the date format with the current date. That date is not stored anywhere else in our system. So “06/04/2021” for client will mean "dd/MM/yyyy" if that's what they were expecting. – bit500 Aug 04 '21 at 15:40

3 Answers3

1

Assuming the date format is always compatible with the patterns accepted by the DateTime.ToString method, you can use a regex to extract the format and do the replace:

string s = "Sample Result {MM/dd/yyyy}";
Regex r = new Regex(@"\{(.*?)\}");
string dateFormat = r.Match(s).Groups[1].Value; //Get the first matching group
string formatted = r.Replace(s,DateTime.Now.ToString(dateFormat));

obviously you should add all the needed null-checks and error handling.

Alberto
  • 15,626
  • 9
  • 43
  • 56
0

Not super ideal due to the variability of the formatting, but if it is always between {} it could work in the following way

Related:

How to get a string between two characters?

and

How to replace the text between two characters in c#

// This is using the two above answers to grab the string between the {}
// and replace with a new date
string originalString = "Sample Result {MM/dd/yyyy}"
string s = originalString;

s = s.substring(s.indexOf("{") + 1);
s = s.substring(0, s.indexOf("}"));

s = DateTime.Now;

Regex regexGoodness = new Regex(@"\{([^\}]+)\}");
originalString = regexGoodness.Replace(originalString, s);
DSMTurboAWD
  • 344
  • 1
  • 3
  • 16
0

Dates and times are always problematic, especially when taking different cultures and time zones into account.

As you specify the date format in the email header could you change it to to store the culture information instead (please forgive the messy code) e.g.

    DateTime date = new DateTime(2021, 10, 31, 17, 4, 32);
    
    string emailHeader = "Sample Result {en-US}";
    
    string cultureString = emailHeader.Substring(emailHeader.IndexOf("{")+1,emailHeader.IndexOf("}")-emailHeader.IndexOf("{")-1);
    CultureInfo culture = new CultureInfo(cultureString);
    
    emailHeader = emailHeader.Replace(cultureString, date.ToString("d", culture));
    Console.WriteLine($"{culture} email header = \"{emailHeader}\"");


    emailHeader = "Sample Result {en-GB}";
    cultureString = emailHeader.Substring(emailHeader.IndexOf("{")+1,emailHeader.IndexOf("}")-emailHeader.IndexOf("{")-1);
    culture = new CultureInfo(cultureString);

    emailHeader = emailHeader.Replace(cultureString, date.ToString("d", culture));
    Console.WriteLine($"{culture} email header = \"{emailHeader}\"");

    

Gives ouput:

en-US email header = "Sample Result {10/31/2021}"
en-GB email header = "Sample Result {31/10/2021}"
ChrisBD
  • 9,104
  • 3
  • 22
  • 35