Reputation: 1291
I am making a quiz app cross-platform using Xamarin and C# in Visual Studio 2017. The problem occurs with this line of code:
DateTime today = new DateTime.Now;
Visual Studio underlines the .Now
and claims that "The type name 'Now' does not exist in the type 'DateTime'."
The code's goal is to get the system time and display it in a textbox (as well as to create a date for the notecard constructor).
Because of .Now
, Visual Studio declares "The type name 'Now' does not exist in the type 'DateTime'.
I also tried using System.DateTime.Now
, but that didn't yield any better result, as the header already includes System
. Below is the template, which is the standard C# for an XAML page:
namespace DenisQuiz.UWP
{
// This page allows the user to create a new study set.
public sealed partial class CreateNewStudySet : Page
{
DateTime today = new DateTime.Now;
public CreateNewStudySet()
{
this.InitializeComponent();
automaticTodaysDate.Text = today.ToString();
}
}
}
This is doubtless a simple solution, but I've looked through the documentation, and no one seems to have this problem, and documentation suggests this exact usage:
DateTime localDate = DateTime.Now;
Is there a way to correct the problem as set up currently, or must I use another method to get the time?
Upvotes: 5
Views: 6144
Reputation: 4550
You are doing wrong here
DateTime today = new DateTime.Now;
You have to new the DateTime Object. https://msdn.microsoft.com/en-us/library/system.datetime(v=vs.110).aspx. In the below example I am passing year , month and day.
DateTime today = new DateTime(2017,12,1); // or other constructor
or simply use which return DateTime instance with current year , current month and current day based on your system/server datetime.
DateTime today = DateTime.Now;
Upvotes: 9