Best format to handle Date/Time data received from a DatePicker - dart

I'm currently implementing a CupertinoDatePicker , so users can pick an expiredate for an item. I'm using that value and pass it through a constructor to add an item to a ListView.
My questions is, what would be the best format to handle that expiredate.
Using it as a pure string sounds easy, but probably not the best way.
I saw that I can pass it to a DateTime , but not sure how convenient that would be.

DateTime works perfect and you can store it as a string no problem.
To decode the string back to datetime simply parse the string:
var expireDate = "2019-30-04 00:00"
var parsedDate = DateTime.parse(expireDate);

Related

What are the JavaScript functions supported in post-processing function

In thingsboard I have a post-processing function for an server attribute update dialog.
update server attribute widget
post-processing date value
post-processing date value fails
I need to convert a text value (entered by the user in a widget) into a unix time stamp (milliseconds precision) to store it into an thingsboard attribute.
I also want do use this function to display the value in a formatted ISO date string. Something linke YYYY-MM-DD hh:mm:ss.
As I understand, the date.getMonth, getFullYear, ... functions are pretty standard for JavaScript. What do I need to do to use them in thingsboard too?
Is there a better way to convert dates?
You have to call use the new-operator to create a date-object.
See Date - JavaScript | MDN:
The only correct way to instantiate a new Date object is by using the new operator. If you simply call the Date object directly, such as now = Date(), the returned value is a string rather than a Date object.
So instead of
var date = Date(value);
it should be
var date = new Date(value);
However, there is a handy and popular javascript date-library called moment.js. Fortunately it is already bundled with Thingsboard and you can use it in widgets and those post-processing functions.

Array in view mvc

I have an array of bytes in my model
public byte[] created_dt { get; set; }
It represents a timestamp value in the database.
In my view , I am referring it as #model.created_dt
but it is coming as system.byte[]
How to resolve this?
Try as this is just an example to achieve the functionality
#foreach(var a in model.created_dt){
<label>#a</label>
}
Judging by the behavior, this looks like ASP.NET and you are simply seeing the output of an implicit call to ToString() on the array (the default way of displaying anything that does not have a template defined). You will have to do something with the raw byte data and present it to the user.
Since you refer to "timestamp" and the property name might suggest a record creation time, you may want to write a helper method to translate this raw data to a DateTime which you could then format accordingly.
However, one of the following is most likely true:
It strikes me as odd that you are using raw binary to store what should otherwise be a datetime2 column. (Or it is a datetime in your database but you're doing something unorthodox to retrieve the value.)
Your property/column name of "created_dt" is a misnomer and it is really a timestamp (i.e. rowversion) column. In which case I don't think this is something a user would know what to do with, and it probably doesn't belong on the UI.

In MVC,using Json to transfer object with time formate property,How to avoid reduce 8 hours automatically?

A student class has a property called birthday. My web Server is in Beijing (UTC+8), and when I get data from the server in JSON format, the birthday property always reduces 8 hours to the original date, How can I correct this? I am also in Beijing and I add 8 hours to all birthday properties, but if I am in another timezone or the web server is not in Beijing then I have to add other hours manually.
On the web server, here's my code:
return Json(student);
The codes that get the data:
var studentReader= new StreamReader(Request.InputStream);
var student= JsonHelper.FromJson<Student>(studentReader.ReadToEnd());
The JsonHelper class:
public static T FromJson<T>(this string json)
{
return JsonConvert.DeserializeObject<T>(json);
}
The recommendation is that, you should store all datetime values in UTC. In the client code, you can convert it to any timezone you want.
Your JSON serialization is fine. No problems there.
You need to make sure to store all date-time values as UTC. And when you display the dates then you can make the conversion to the Timezone you need.
Here's how I usually do it:
Every time I store a DateTime I use DateTime.ToUniversalTime() to make sure the date is stored on UTC on the database.
When I need to print the date in a View I have a method that I used that takes a specific TimeZone and converts the date. See bellow for a code example.
I never rely on Client-Side zones. If I have a broad range of users from many geographical points I just make then choose their preferred timezone and store this information on their profiles (Google, Microsoft, and 37Signals all do this for their web applications just to name a few examples).
public static DateTime ToLocalTimeZone(this object date)
{
DateTime dt;
if (!DateTime.TryParse(date.ToString(), out dt))
return DateTime.UtcNow;
//Timezone string. This can be stored on web.config
//or on the user profile as well
var timeZone = "Pacific SA Standard Time";
var defaultTimeZone = TimeZoneInfo.FindSystemTimeZoneById(timeZone);
var userTime = TimeZoneInfo.ConvertTimeFromUtc(dt, defaultTimeZone);
return userTime;
}
More info on previous questions here and here
Edit: List of allowed Timezone strings here

Restkit custom mapping

Wondering if its possible to concatenate keyPaths to one attribute in mapping objects. Looking for something like
mapKeyPaths #"firstname", #"lastname", nil toAttribute:#"name"
Where name would then be "Bob Johnson"
** The API I am dealing with passes over a date and a startTime attribute, as 2012/02/28 and 16:12 respectively, as Strings.
It would be easier to just use startTime as "2012/02/28 16:12".
I figured I can get around this issue by leaving the date and startTime as NSDate fields, so I have tried setting up a dateFormatter per Restkits instructions. When I tried that idea, just using "HH:MM", for the startTime dateFormatter, it shoves "1970/01/01 16:12" into the startTime field.
Anyone have any suggestions, besides going through each record manually after mapping to Core Data and putting the fields in programatically?
I don't think you can do these kind of programmatic mappings yet.
Two alternative solutions come to mind:
1) In willMapData (or something like that) you can manually modify the incoming serialization before object mapping occurs. There you can specify a format you like.
2) Save both these properties in your Core Data entity and create a third transient attribute which is calculated at runtime, and when required, by passing these two values through a NSDateFormatter.

difference between string and string builder

I am having doubt in whether to use string or string builder to append html elements like "div" and others in my page in mvc. Is there any other approach for this thing.
Thanks.
I read that Microsoft recommends using StringBuilder when you predict to have more then 6 concatenations.
StringBuilder is the way to go. A String holds a reference to an immutable (fixed) string, and appending to a string is horribly inefficient. If your intention is to repeated perform appends then this is exactly what the StringBuilder was designed for.
You should use StringBuilder if you change string a lot (add, remove, change, replace characters) because it's more efficient. If you do simply operation you should use string.
The problem with string is that it's immutable, so operatrion
string text = myStringVariable + "new string"
causes that the new instance of the text variable will be created. If you do many operation on string class then you will have many instances of string objects.
Whenever you have perform appending texts, you should always use stringbuilder.
Using string would repeatedly create new instances of a string and hence inefficient.
check this post for the depth knowledge about : Why to use StringBuilder over string to get better performance
To be honest and saying something unusual at the end it really does not matter. The differences are so small that you shouldn't care about this and you should invest the time in other things that make difference.
Check this article of Jeff where all this is explained (also in a web environment, when he was creating StackOverflow).
Coding horror article about why does not matter how do you create strings
String bulider Can Be Used When More than One String to be
concatenated.
StringBuilder which is more efficient because it does
contain a mutable string buffer. .NET Strings are immutable
which is the reason why a new string object is created
every time we alter it (insert, append, remove, etc.).

Resources