Dynamically replace string with Current Date with specific format using C# and VB.Net

Last Reply one year ago By pandeyism

Posted one year ago

Hi,

i am writing a program to send mail using VB.net console application and i want to define mail subject in app config file like so that user can change date format as per their requirement.

Subject :  @date[dd-MM-YYYY] : FTP Reports

After defining above vb.net code should convert the subject as per defined date format  like below.

Example :  Subject :  20-11-2018 : FTP Reports

    jsonArrayString = File.ReadAllText(FileCopyPath & ar & "_" & Format(Now(), "ddMMyyyy") & ".json")
    Dim list = JsonConvert.DeserializeObject(jsonArrayString)
    json = JObject.Parse(list.ToString)
    Dim TOmail As String = json(ar)(0)("To").ToString()
    Dim subjectsobvert As String = "@date[dd-MM-YYYY] : FTP Reports"
    Dim dt As DateTime = Convert.ToDateTime(subjectsobvert, New CultureInfo("en-GB"))
    Dim mail As New MailMessage
    mail.Subject = dt

 

Posted one year ago

Hey Nikhkhot,

Please refer below code.

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    string s = "@date[DD-MM-YYYY] : FTP Reports";
    string format = s.Split('[')[1].Split(']')[0].ToLower().Replace("mmm", "MMM").Replace("mm", "MM");
    string Subject = s.Replace("@date[" + s.Split('[')[1].Split(']')[0] + "]", DateTime.Now.ToString(format));
    Response.Write(Subject);
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    Dim s As String = "@date[DD-MM-YYYY] : FTP Reports"
    Dim format As String = s.Split("["c)(1).Split("]"c)(0).ToLower().Replace("mmm", "MMM").Replace("mm", "MM")
    Dim Subject As String = s.Replace("@date[" & s.Split("["c)(1).Split("]"c)(0) & "]", DateTime.Now.ToString(format))
    Response.Write(Subject)
End Sub

Output

22-11-2018 : FTP Reports