For whatever reason the data I am getting from database is coming in this format: " 830AM"
Also notice that extra space before number 8
I need to format it to show as "08:30 AM" in Silverlight XAML or Styles.
How would we do that?
If you're sure that the incoming format is always the same, you could use DateTime.ParseExact to get the DateTime object -- then convert it back to a string with the format you want.
string timeStr = " 830AM";
string format = "h:mmtt";
DateTime time = DateTime.ParseExact(timeStr.Trim(), format, CultureInfo.InvariantCulture);
string timeStrFormatted = time.ToString("hh:mm tt", CultureInfo.InvariantCulture);
To do this in Silverlight XAML, you could use an IValueConverter in the binding. Better still, use a read-only property on the view-model, which performs the above conversion.
Related
I'm struggling for hours with this seemingly trivial issue.
I have a antd datepicker on my page.
Whenever I choose a date, instead of giving me the date I chose, it gives me a messy moment object, which I can't figure out how to read.
All I want is that when I choose "2020-01-18", it should give me precisely this string that the user chose, regardless of timezone, preferably in ISO format.
This is not a multi-national website. I just need a plain vanilla date so I can send it to the server, store in db, whatever.
Here are some of my trials, so far no luck:
var fltval = e;
if (isMoment(fltval)) {
var dat = fltval.toDate();
//dat.setUTCHours(0)
fltval = dat.toISOString(); // fltval.toISOString(false)
var a = dat.toUTCString();
//var b = dat.toLocaleString()
}
It keeps on moving with a few hours, probably to compensate for some timezone bias
UPDATE 1:
the datestring is data-wise correct. But its not ISO, so I cant use it correctly. I might try to parse this, but I cannot find a way to parse a string to date with a specific format.
UPDATE 2:
I also tried adding the bias manually, but for some reason the bias is 0
var dat = pickerval.toDate()
var bias = Date.prototype.getTimezoneOffset()// this is 0...
var bias2 = dat.getTimezoneOffset()// and this too is 0
var d2 = new Date(dat.getTime()+bias)
var mystring= dat.toISOString() //still wrong
Thanks!
Javascript date functions can be used,
I assume you are getting in 2022-01-03T11:19:07.946Z format then
date.toISOString().slice(0, 10)
to 2022-01-03
There are 2 ways to get the date string:
Use the moment.format api:
date.format("yyyy-MM-DD")
Use the date string that is passed to the onChange as second parameter
Here is a Link.
I am assuming your code snippet is inside the onChange method. This gives you a moment and a date string to work with (the first and second parameters of the function respectively).
You have a few options. You could set the format prop on the DatePicker to match the format of the string you want. Then just use the date string. Or you can use the moment object as Domino987 described.
This is input format:
yyyy:MM:dd'T'HH:mm:ss'Z' (Coming as a string from json service)
Required output format:
dd-mmm-yyyy
I have tried with {{txnDate | date:'dd-mm-yyyy'}}
but it is not working..
What is the format you are following for your date?
A quick var a = new Date(); a.toISOString(); in console will give you something like "2015-02-19T13:30:13.347Z". The formatted string you are receiving is not following any standard and I am afraid parsing it to date will result in Invalid Date in most of the browsers.
So you can either
Get your Date in proper format.
Make the best use of whatever is available. You can use split to break your string into individual components.
Something like:
var a = "yyyy:MM:dd'T'HH:mm:ss'Z'" //Replace with actual string
b=a.split(':') will result in ["yyyy", "MM", "dd'T'HH", "mm", "ss'Z'"] giving you year and months in b[0] and b[1].
For date, you can use b[2].substring(0,2) to give you dd.
You have all date components(apart from time components, which you don't need anyway) as string.
Either use them directly(as a string) or make a date object using these components(since you want month in MMM format).
$scope.txnDate = new Date(b[0]+'/'+b[1]+'/'+b[2].substring(0,2));
I am sure there are more ways to optimize this. Comment if this doesn't work for you, will try to elaborate more.
I want to format a number like this :
100000000 => 100 000 000 or -1500 => -1 500 or -500 => -500
For now I'm using this method, but it doesn't work well with negative values :
<TextBlock Text="{Binding Path=Value, StringFormat='### ### ##0'}" />
For example with : -500 I got - 500 (two spaces between '-' and '500')
Any hints ? Is it possible to do this without a converter ?
Thanks.
I don't think you can avoid a converter in this case. You current use of spaces are treated as literals and are just injected into the final number at those character positions.
The correct format string is actually "#,##0". The "," is actuall a directive it group digits in three digit groups and place a separator string between them. The actual separator string uses is taken from the NumberGroupSeparator string property of the NumberFormat of the CultureInfo class being used by xaml. Normally this is from the "en-US" culture and will be a comma.
There is no way to specify a custom instance of CultureInfo to be used so you can't manipulate the NumberGroupSeparator. Hence you will need to use an instance of a IValueConverter.
This console app code generates appropriate output
NumberFormatInfo nfi = new CultureInfo(String.Empty).NumberFormat;
nfi.NumberGroupSeparator = " ";
Console.WriteLine((100000000).ToString("#,##0",nfi));
Console.WriteLine((-500).ToString("#,##0",nfi));
Technology: Visual Studio 2008 .NET, Winforms
bsTransactions.DataSource = Transactions.Tables[2];
bsnTransactions.BindingSource = bsTransactions;
txtTransOverrideDate.DataBindings.Add("Text", bsTransactions, "TransactionDate", true,DataSourceUpdateMode.Never, "", "MM/dd/yyyy");
Currently, I'm getting an error saying that the string wasn't a recognized DateTime string. Despite the fact that if I get the column type from
Transactions.Tables[2].Rows[0]["TransactionDate"].DataType.ToString();
returns "System.DateTime" and the actual value looks like "1/23/2010 12:00:00 AM"
I'm trying to format a string that currently looks like "1/23/2010 12:00:00 AM" to only show the date.
The catch is, there's a BindingSourceNavigator being use, so just applying the formatting to the textbox after the fact only applies to the first value, but not any others that are navigated to using the bindingNavigator.
I have a feeling that using the "bsTransactions" to applies bindings to the textbox, it's changing the datatype, hence it not being recognized as a DateTime.
txtTransOverrideDate.DataBindings.Add("Text", bsTransactions, "TransactionDate", true,DataSourceUpdateMode.Never, "", "MM/dd/yyyy");
I made changes in the original post to mimic this, but this is the answer.
I want to use a Wpf RichTextBox to edit data and resave it to SQL field.
I need of course the text to be saved as rich text, not just simple text of course.
What data-type should be best for storing in SQL server (2005 - forget about file stream)?
How do I save/retrieve it?
You can save the document to a XAML string using XamlWriter :
StringWriter wr = new StringWriter();
XamlWriter.Save(richTextBox.Document, wr);
string xaml = wr.ToString();
You can then save the XAML string to a the database like any other text.
To reload it from a XAML string, use XamlReader :
FlowDocument doc = XamlReader.Parse(xaml) as FlowDocument;
The datatype in SQL should probably be NVARCHAR(MAX): Nvarchar means you can store unicode, and MAX means you can store unlimited amounts, (well up to 2GB anyway) of data.