How to convert only date and month to string from CalendarDatePicker
![Multi tool use Multi tool use](http://sgv.ssvwv.com/sg/ssvwvcomimagb.png)
Multi tool use
I am writing a simple code in UWP where the user can select date from CalendarDatePicker and the app will show the selected date in a TextBlock and do some functionalities. How can I do that?
I have tried to do it with ToString() method. It shows full form of the date (ex: 1/1/2019 10:27 AM +06:00)
But I want to show the date and the month only.
Here is the code that I've written:
XAML
<CalendarDatePicker Name="calDatePicker"
DateChanged="calDatePicker_DateChanged"
Grid.Column="1"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Margin="5,0,0,1"/>
<TextBlock Name="dateText"
Style="{StaticResource sampleText}"
Text="Preferred Time"/>
C#:
private void calDatePicker_DateChanged(CalendarDatePicker sender, CalendarDatePickerDateChangedEventArgs args)
{
if (calDatePicker.Date < DateTime.Now)
{
dateText.Text = "Order must be placed between this day and next 5 days.";
}
else
{
dateText.Text = calDatePicker.Date.ToString();
counter = true;
}
}
c# xaml datetime uwp uwp-xaml
add a comment |
I am writing a simple code in UWP where the user can select date from CalendarDatePicker and the app will show the selected date in a TextBlock and do some functionalities. How can I do that?
I have tried to do it with ToString() method. It shows full form of the date (ex: 1/1/2019 10:27 AM +06:00)
But I want to show the date and the month only.
Here is the code that I've written:
XAML
<CalendarDatePicker Name="calDatePicker"
DateChanged="calDatePicker_DateChanged"
Grid.Column="1"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Margin="5,0,0,1"/>
<TextBlock Name="dateText"
Style="{StaticResource sampleText}"
Text="Preferred Time"/>
C#:
private void calDatePicker_DateChanged(CalendarDatePicker sender, CalendarDatePickerDateChangedEventArgs args)
{
if (calDatePicker.Date < DateTime.Now)
{
dateText.Text = "Order must be placed between this day and next 5 days.";
}
else
{
dateText.Text = calDatePicker.Date.ToString();
counter = true;
}
}
c# xaml datetime uwp uwp-xaml
This is a little problematic functionality. You can useToLongDateString()
orToShortDateString()
to display only a full date. But when you want do display only year and month, you need to use your specific format inToString
function. But there is problem with international compatibility. There is no way to get format for month and day only writing from system or support classes. It can beMM/dd
(e.g. USA), ordd. MM.
(e.g. many European countries) orMM月dd日
(Japan, and perhaps China). And therefore you need to determine custom format, or remove year from country format.
– Julo
Jan 1 at 4:51
Possible duplicate of C# DateTime to "YYYYMMDDHHMMSS" format
– Mohamad Armoon
Jan 1 at 4:56
add a comment |
I am writing a simple code in UWP where the user can select date from CalendarDatePicker and the app will show the selected date in a TextBlock and do some functionalities. How can I do that?
I have tried to do it with ToString() method. It shows full form of the date (ex: 1/1/2019 10:27 AM +06:00)
But I want to show the date and the month only.
Here is the code that I've written:
XAML
<CalendarDatePicker Name="calDatePicker"
DateChanged="calDatePicker_DateChanged"
Grid.Column="1"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Margin="5,0,0,1"/>
<TextBlock Name="dateText"
Style="{StaticResource sampleText}"
Text="Preferred Time"/>
C#:
private void calDatePicker_DateChanged(CalendarDatePicker sender, CalendarDatePickerDateChangedEventArgs args)
{
if (calDatePicker.Date < DateTime.Now)
{
dateText.Text = "Order must be placed between this day and next 5 days.";
}
else
{
dateText.Text = calDatePicker.Date.ToString();
counter = true;
}
}
c# xaml datetime uwp uwp-xaml
I am writing a simple code in UWP where the user can select date from CalendarDatePicker and the app will show the selected date in a TextBlock and do some functionalities. How can I do that?
I have tried to do it with ToString() method. It shows full form of the date (ex: 1/1/2019 10:27 AM +06:00)
But I want to show the date and the month only.
Here is the code that I've written:
XAML
<CalendarDatePicker Name="calDatePicker"
DateChanged="calDatePicker_DateChanged"
Grid.Column="1"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Margin="5,0,0,1"/>
<TextBlock Name="dateText"
Style="{StaticResource sampleText}"
Text="Preferred Time"/>
C#:
private void calDatePicker_DateChanged(CalendarDatePicker sender, CalendarDatePickerDateChangedEventArgs args)
{
if (calDatePicker.Date < DateTime.Now)
{
dateText.Text = "Order must be placed between this day and next 5 days.";
}
else
{
dateText.Text = calDatePicker.Date.ToString();
counter = true;
}
}
c# xaml datetime uwp uwp-xaml
c# xaml datetime uwp uwp-xaml
edited Jan 3 at 11:28
marc_s
577k12911141259
577k12911141259
asked Jan 1 at 4:37
Mahmudul HasanMahmudul Hasan
53
53
This is a little problematic functionality. You can useToLongDateString()
orToShortDateString()
to display only a full date. But when you want do display only year and month, you need to use your specific format inToString
function. But there is problem with international compatibility. There is no way to get format for month and day only writing from system or support classes. It can beMM/dd
(e.g. USA), ordd. MM.
(e.g. many European countries) orMM月dd日
(Japan, and perhaps China). And therefore you need to determine custom format, or remove year from country format.
– Julo
Jan 1 at 4:51
Possible duplicate of C# DateTime to "YYYYMMDDHHMMSS" format
– Mohamad Armoon
Jan 1 at 4:56
add a comment |
This is a little problematic functionality. You can useToLongDateString()
orToShortDateString()
to display only a full date. But when you want do display only year and month, you need to use your specific format inToString
function. But there is problem with international compatibility. There is no way to get format for month and day only writing from system or support classes. It can beMM/dd
(e.g. USA), ordd. MM.
(e.g. many European countries) orMM月dd日
(Japan, and perhaps China). And therefore you need to determine custom format, or remove year from country format.
– Julo
Jan 1 at 4:51
Possible duplicate of C# DateTime to "YYYYMMDDHHMMSS" format
– Mohamad Armoon
Jan 1 at 4:56
This is a little problematic functionality. You can use
ToLongDateString()
or ToShortDateString()
to display only a full date. But when you want do display only year and month, you need to use your specific format in ToString
function. But there is problem with international compatibility. There is no way to get format for month and day only writing from system or support classes. It can be MM/dd
(e.g. USA), or dd. MM.
(e.g. many European countries) or MM月dd日
(Japan, and perhaps China). And therefore you need to determine custom format, or remove year from country format.– Julo
Jan 1 at 4:51
This is a little problematic functionality. You can use
ToLongDateString()
or ToShortDateString()
to display only a full date. But when you want do display only year and month, you need to use your specific format in ToString
function. But there is problem with international compatibility. There is no way to get format for month and day only writing from system or support classes. It can be MM/dd
(e.g. USA), or dd. MM.
(e.g. many European countries) or MM月dd日
(Japan, and perhaps China). And therefore you need to determine custom format, or remove year from country format.– Julo
Jan 1 at 4:51
Possible duplicate of C# DateTime to "YYYYMMDDHHMMSS" format
– Mohamad Armoon
Jan 1 at 4:56
Possible duplicate of C# DateTime to "YYYYMMDDHHMMSS" format
– Mohamad Armoon
Jan 1 at 4:56
add a comment |
1 Answer
1
active
oldest
votes
private void calDatePicker_DateChanged(CalendarDatePicker sender, CalendarDatePickerDateChangedEventArgs args)
{
if (calDatePicker.Date < DateTime.Now)
{
dateText.Text = "Order must be placed between this day and next 5 days.";
}
else
{
var dt = calDatePicker.Date;
dateText.Text = dt.Value.Year.ToString() + " " + dt.Value.Month.ToString();
counter = true;
}
}
I used year and month for example, you can use everything you need.
– CodeMan
Jan 1 at 4:47
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53993038%2fhow-to-convert-only-date-and-month-to-string-from-calendardatepicker%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
private void calDatePicker_DateChanged(CalendarDatePicker sender, CalendarDatePickerDateChangedEventArgs args)
{
if (calDatePicker.Date < DateTime.Now)
{
dateText.Text = "Order must be placed between this day and next 5 days.";
}
else
{
var dt = calDatePicker.Date;
dateText.Text = dt.Value.Year.ToString() + " " + dt.Value.Month.ToString();
counter = true;
}
}
I used year and month for example, you can use everything you need.
– CodeMan
Jan 1 at 4:47
add a comment |
private void calDatePicker_DateChanged(CalendarDatePicker sender, CalendarDatePickerDateChangedEventArgs args)
{
if (calDatePicker.Date < DateTime.Now)
{
dateText.Text = "Order must be placed between this day and next 5 days.";
}
else
{
var dt = calDatePicker.Date;
dateText.Text = dt.Value.Year.ToString() + " " + dt.Value.Month.ToString();
counter = true;
}
}
I used year and month for example, you can use everything you need.
– CodeMan
Jan 1 at 4:47
add a comment |
private void calDatePicker_DateChanged(CalendarDatePicker sender, CalendarDatePickerDateChangedEventArgs args)
{
if (calDatePicker.Date < DateTime.Now)
{
dateText.Text = "Order must be placed between this day and next 5 days.";
}
else
{
var dt = calDatePicker.Date;
dateText.Text = dt.Value.Year.ToString() + " " + dt.Value.Month.ToString();
counter = true;
}
}
private void calDatePicker_DateChanged(CalendarDatePicker sender, CalendarDatePickerDateChangedEventArgs args)
{
if (calDatePicker.Date < DateTime.Now)
{
dateText.Text = "Order must be placed between this day and next 5 days.";
}
else
{
var dt = calDatePicker.Date;
dateText.Text = dt.Value.Year.ToString() + " " + dt.Value.Month.ToString();
counter = true;
}
}
edited Jan 1 at 5:12
answered Jan 1 at 4:46
![](https://i.stack.imgur.com/CQCVp.jpg?s=32&g=1)
![](https://i.stack.imgur.com/CQCVp.jpg?s=32&g=1)
CodeManCodeMan
557311
557311
I used year and month for example, you can use everything you need.
– CodeMan
Jan 1 at 4:47
add a comment |
I used year and month for example, you can use everything you need.
– CodeMan
Jan 1 at 4:47
I used year and month for example, you can use everything you need.
– CodeMan
Jan 1 at 4:47
I used year and month for example, you can use everything you need.
– CodeMan
Jan 1 at 4:47
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53993038%2fhow-to-convert-only-date-and-month-to-string-from-calendardatepicker%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
v4zFmc x 4dK,rFyWm,8aujPc,WFe1Q4O8,aA,52JyZN,2VxF Lrl,Cx2 i4
This is a little problematic functionality. You can use
ToLongDateString()
orToShortDateString()
to display only a full date. But when you want do display only year and month, you need to use your specific format inToString
function. But there is problem with international compatibility. There is no way to get format for month and day only writing from system or support classes. It can beMM/dd
(e.g. USA), ordd. MM.
(e.g. many European countries) orMM月dd日
(Japan, and perhaps China). And therefore you need to determine custom format, or remove year from country format.– Julo
Jan 1 at 4:51
Possible duplicate of C# DateTime to "YYYYMMDDHHMMSS" format
– Mohamad Armoon
Jan 1 at 4:56