Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Lex Godthelp 15 posts 99 karma points
    Feb 06, 2017 @ 16:25
    Lex Godthelp
    0

    Razor - Adding a comma after 2 characters of a string

    Hi guys,

    Quick question. I'd like to add a

    :

    In between a string i have. Now i have a string that goes like this:

    @((item.GetValue("starttijdwebinar")-100))
    

    This outputs into:

    e.g. 2100
    

    Now i'd like to make it so this will output with a ; after the first 2 characters like this:

    e.g. 21:00
    

    Do you guys have any quick suggestions?

  • Kevin Jump 2312 posts 14698 karma points MVP 7x c-trib
    Feb 06, 2017 @ 16:37
    Kevin Jump
    0

    hi.

    string.Format("{0:00':'00}", value);
    

    will output in the correct format.

    but if you are manipulating date functions, you are probably better putting everything into a DateTime value - and using the time manipulation functions (e.g date.AddHours(-1) ) as that will copy with all the strange things (like taking an hour from 24 mins past midnight on the first of a month.).

  • Lex Godthelp 15 posts 99 karma points
    Feb 06, 2017 @ 16:46
    Lex Godthelp
    0

    Hi Kevin,

    Thank you for your response. Might be a stupid question but how do i work this into my code?

    I've made it so that i can now call a var like this:

    var promotietijdwebinar = (item.GetValue("starttijdwebinar")-100);
    

    Can i format this with string.format?

  • Kevin Jump 2312 posts 14698 karma points MVP 7x c-trib
    Feb 06, 2017 @ 16:50
    Kevin Jump
    0

    hi ,

    i am not 100% sure where you are in your code and what object item is but...

    var promotietijdwebinar = 
              string.format("{0:00':'00}", (item.GetValue("starttijdwebinar")-100));
    

    would work i think - but it would make promotietijdwebinar a string, when it's previously an int - this might not be an issue but if it is...

    replacing it where you write it out. is the other option ...

    @promotietijdwebinar 
    

    could become

    @(string.format("{0:00':'00'}", promotietijdwebinar )) 
    

    and this would also work.

    Kevin

  • Lex Godthelp 15 posts 99 karma points
    Feb 06, 2017 @ 17:02
    Lex Godthelp
    0

    Hi Kevin,

    I'm sorry but its not working for me. Maybe that's because value starttijdwebinar isn't a DateTime?

    Here's the entire code:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @{
        var startNodeId = 1819;
        var startNode = Umbraco.Content(startNodeId);
        var currentdate = DateTime.Now.ToString("yyyy-MM-dd");
        var currenttime = DateTime.Now.ToString("HHmm");
        var selection = startNode.Webinarcontentgrid;
        int i = 1;
    
    foreach (var item in selection){
        if(item.contentgriddate >= currentdate && item.eindtijdwebinar > currenttime ){
            if(@i <= 1){
                var promotietijdwebinar = (item.GetValue("starttijdwebinar")-100);
    
                if(item.starttijdwebinar > currenttime && @promotietijdwebinar > currenttime){
                    <div class="wrapper">
                        <img src="https://webinar.leadkeeper.net/media/129038/background.jpg" alt="background img Microsoft Sessies" />
                            <div class="streammessage">
                                <img src="https://webinar.leadkeeper.net/media/129040/icon_wachten.png" alt="not started" />
                                <h1>De uitzending op deze pagina is nog niet begonnen.</h1>
                                <p>Inloggen is mogelijk op <strong>@item.contentgriddate</strong> vanaf <strong>@promotietijdwebinar</strong>.<br />
                                De uitzending start om <strong>@item.starttijdwebinar</strong>.</p>
                            </div>
                    </div>
                }
            } i++;
        }
    }           
    

    }

    Values eindtijdwebinar and starttijdwebinar are plain text values.

  • Kevin Jump 2312 posts 14698 karma points MVP 7x c-trib
    Feb 06, 2017 @ 17:13
    Kevin Jump
    0

    Hi,

    you have an @ in the if line .

            if(item.starttijdwebinar > currenttime && @promotietijdwebinar > currenttime){
    

    so remove that.

            if(item.starttijdwebinar > currenttime && promotietijdwebinar > currenttime){
    

    also, if you only care about how it's displayed you only need to put the formmating on the promotietijdwebinar value in the paragraph ?

    <h1>De uitzending op deze pagina is nog niet begonnen.</h1>
    <p>Inloggen is mogelijk op <strong>@item.contentgriddate</strong> vanaf <strong>@promotietijdwebinar</strong>.<br />
    De uitzending start om <strong>@item.starttijdwebinar</strong>.</p>
    

    so

    <strong>@(string.format("{0:00':'00}", promotietijdwebinar)</strong>
    

    should be enough ?

    this formatting only works when the value is an int.

    so either check the values are int,s or see if you can get them by type (for example with item.GetValue<int>("thing"))

    or you could parse them int.Parse(value) if they are strings.

    but i would have checking around a lot of this, because things like parse could fail..

Please Sign in or register to post replies

Write your reply to:

Draft