Answered Operazioni su variabile data

  • Monday, March 12, 2012 12:56 PM
     
     

    Ciao a tutti, avrei necessità di sapere come in asp net posso ottenere queste informazioni da variabili:

    1. data corrente (fto.: Giorno, Nome Mese, AAAA);
    2. data corrente anno precedente (fto.: Giorno, Nome Mese, AAAA -precedente-);
    3. ultimo mese anno precedente (fto.: Nome mese, AAAA -precedente-).

    Sapete aiutarmi?

    Grazie in anticipo.

    Cordialità





    • Edited by cms9651 Monday, March 12, 2012 12:58 PM
    • Edited by cms9651 Monday, March 12, 2012 12:58 PM
    • Edited by cms9651 Monday, March 12, 2012 1:03 PM
    • Edited by cms9651 Monday, March 12, 2012 1:04 PM
    •  

All Replies

  • Monday, March 12, 2012 1:21 PM
     
      Has Code

    La classe DateTime ti fornisce un pò di cosette:

    string s1 = DateTime.Now.ToString("d MMMM yyy");
    
    string s2 = DateTime.Now.AddYears(-1).ToString("d MMMM yyy");
    
    DateTime dateTime = new DateTime(DateTime.Now.AddYears(-1).Year, 12, DateTime.Now.Day);
    string s3 = dateTime.ToString("d MMMM yyy");

    Comuque dai un'occhiata qui per farti un idea di come formattare le date

    http://msdn.microsoft.com/en-us/library/az4se3k1.aspx 

    Orchard Mango Theme


  • Monday, March 12, 2012 1:37 PM
     
      Has Code

    Grazie Marco per il prezioso aiuto.

    Con le tue indicazioni ho buttato già giù qualche riga di codice.

    Ma ottengo questo alert, perchè? Cosa sbaglio? Cosa non ho capito?

    Grazie di nuovo.

    A chart element with the name 'December 2011' could not be found in the 'SeriesCollection'

    DateTime dateTime = new DateTime(DateTime.Now.AddYears(-1).Year, 12, DateTime.Now.Day);
    string s3 = dateTime.ToString("MMMM yyy");
    Chart1.Series[s3].ChartType = SeriesChartType.Column;
    
    ...
    
    
    string s2 = DateTime.Now.AddYears(-1).ToString("d MMMM yyy");
    Chart1.Series[s2].ChartType = SeriesChartType.Column;
    
    ...
    
    string s1 = DateTime.Now.ToString("d MMMM yyy");
    Chart1.Series[s1].ChartType = SeriesChartType.Column;
    
    ...
    
    <Series>
    <asp:Series Name="<%=s3%>" ChartArea="ChartArea1">
    </asp:Series>
    </Series>
    
    <Series>
    <asp:Series Name="<%=s2%>" ChartArea="ChartArea1">
    </asp:Series>
    </Series>
    
    <Series>
    <asp:Series Name="<%=s1%>" ChartArea="ChartArea1">
    </asp:Series>
    </Series> 



    • Edited by cms9651 Monday, March 12, 2012 1:37 PM
    • Edited by cms9651 Monday, March 12, 2012 1:40 PM
    •  
  • Monday, March 12, 2012 2:17 PM
     
     
    L'errore che hai non è relativo alla formattazione delle date... piuttosto all'utlizzo del controllo Chart, che devi fare di preciso? :)

    Orchard Mango Theme

  • Monday, March 12, 2012 2:40 PM
     
      Has Code

    Grazie per la risposta.

    Dovrei assegnare alle Series Name il nome già assegnato con la formattazione delle date che mi hai suggerito.

    Avevo pensato di risolvere così, cioè inserendo nell'html della pagina net il nome della variabile string prevista in C#, ma restiutisce l'errore che ti ho segnalato:

    DateTime dateTime = new DateTime(DateTime.Now.AddYears(-1).Year, 12,

    DateTime.Now.Day); string s3 = dateTime.ToString("MMMM yyy"); Chart1.Series[s3].ChartType = SeriesChartType.Column;

    ...

    <Series> <asp:Series Name="<%=s3%>" ChartArea="ChartArea1"> </asp:Series> </Series>


  • Monday, March 12, 2012 3:07 PM
     
     Answered Has Code

    Perdonami ma nel ASPX richiami proprio la variabile "s3" dichiarata e valorizzata in una funzione del CodeBehind? Se è così dubito che funzioni... se proprio vuoi agire in quel modo dovresti lavorare con delle property chesso:

    public string SerieName { get; set; }
    <Series>
    <asp:Series Name="<%=this.SerieName%>" ChartArea="ChartArea1">
    </asp:Series>
    </Series>
    

    Anche se per quanto mi riguarda a questo punto imposterei direttamente il nome della serie nel CodeBehind..


    Orchard Mango Theme

    • Marked As Answer by cms9651 Monday, March 12, 2012 3:44 PM
    •  
  • Monday, March 12, 2012 3:44 PM
     
      Has Code

    Anche se per quanto mi riguarda a questo punto imposterei direttamente il nome della serie nel CodeBehind..


    Orchard Mango Theme

    Sei stato illuminante!

    Mille grazie, ho risolto impostando direttamente il nome della serie nel CodeBehind:

           DateTime dateTime = new DateTime(DateTime.Now.AddYears(-1).Year, 12, DateTime.Now.Day);
           string s3 = dateTime.ToString("MMMM yyy");
           Chart1.Series.Add(s3);
    Chart1.Series[s3].ChartType = SeriesChartType.Column;
    ...
    <asp:Chart ID="Chart1" runat="server" Width="1058" Height="680">
    </asp:Chart>
    ...