How to Convert .NET DateTime to JSON? - ios

createdBy: "f201b814-34c2-4948-9ab2-36a2293d7098"
eventID: 5
forumDescription: "Android Forum 01 Description"
forumID: 1
forumStatus: 3
forumTitle: "Android Forum 01"
lastModifiedDateTime: "/Date(1413427128000+0530)/"
version: 3
}
1: {
createdBy: "f201b814-34c2-4948-9ab2-36a2293d7098"
eventID: 5
forumDescription: "Android sdasdasd 02"
forumID: 2
forumStatus: 3
forumTitle: "Android Forum 02"
lastModifiedDateTime: "/Date(1413427135000+0530)/"
version: 3

Create Unix timestamp and then use that. Unix timestamp is count of millisecond from 1/1/1970.
For creating timestamp in C# use this:
public string ToUnixEpoch(DateTime dateTime)
{
DateTime d1 = new DateTime(1970, 1, 1);
DateTime d2 = dateTime.ToUniversalTime();
TimeSpan ts = new TimeSpan(d2.Ticks - d1.Ticks);
return ts.TotalMilliseconds.ToString("#");
}
For more info see this. Hope this helps... :)

Related

how to assign created_at only date string in laravel

I Have a problem with the created_at and other carbon dates, when I do
return $return_items['created_date'] = $item->created_at;
It returns '2016-07-26 13:19:55'
but if I do
return $return_items;
it returns
"created_date": {
"date": "2016-07-26 13:22:17.000000"
"timezone_type": 3
"timezone": "UTC"
}
How can I solve this, to only show the string date?
thanks
$object->created_at->timestamp
$object->created_at->format('d M Y - H:i:s')
Try this below code,
$dateOnly = new DateTime($item->created_at);
echo $dateOnly ->format('Y-m-d');
For example, display the date in the format you want and the hour, minutes and seconds :
{{ $user->created_at->format('d M Y - H:i:s') }}
Or only date:
{{ $user->created_at->format('d M Y') }}
More info, in the documentation Laravel Date Casting and Carbon

Can I Compare the day name with the date Entity Framework 6

I am working on news function. News model contains publishing date....
Is there a way to filter my record from db on the base of Publishing Date's day name such as in controller action:
var data1 = db.News.Where(x => x.PublishingDate >= DateTime.Now
&& x.PublishingDate.Day == (int)DayOfWeek.Sunday);
ViewBag.SundayNews = data1;
Or if there is another way around or any reference.
Try this solution: http://c-sharp-snippets.blogspot.ru/2011/12/getting-dayofweek-in-linq-to-entities.html
var firstSunday = new DateTime(1753, 1, 7);
var filtered = from e in dbContext.Entities
where EntityFunctions.DiffDays(firstSunday, e.SomeDate) % 7 == (int)DayOfWeek.Monday
select e;
firstSunday stores the minimal value for MS SQL DATETIME type.

How to change the datetime value to year only, that retrieve from database table and show on combobox.display member?

i got a list of datetime value from ()table look like these: 31/12/14 4:45:30 PM
31/12/14 4:45:30 PM
31/12/14 4:45:30 PM
31/12/14 4:45:30 PM
31/12/14 4:45:30 PM
AND i want them to only show like e.g.:, in the combobox.display member
2014
2015
2016
2017
i know i wrote like below might have datatypes error, but anyone can correct me or guide me on this??
Public Sub combothing()
Dim connection As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & aaa & "';Persist Security Info=False;")
Dim selectme As String = "SELECT * FROM yeartbl"
Dim cmd As New OleDbCommand(selectme, connection)
Dim da As New OleDbDataAdapter
Dim ds As New DataSet
Dim dt As New DataTable
Try
If connection.State = ConnectionState.Closed Then
connection.Open()
End If
da.SelectCommand = cmd
da.Fill(ds)
da.Fill(ds, "yeartbl")
dt = ds.Tables("yeartbl")
connection.Close()
Me.ComboBox1.DataSource = ds.Tables(0)
Me.ComboBox1.ValueMember = "YID"
**Convert.ToDateTime(Me.ComboBox1.DisplayMember = "Years").Date.Year.ToString()**
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
One of the things that you can do (assuming we work against Excel) is, when you already filled DataSet, add one more column
ds.Tables(0).Columns.Add("year", System.Type.GetType("System.Integer"))
For each (r as row in ds.Tables(0).Rows)
r("year") = DirectCast(r("YTD"), DateTime).Year 'assuming it is not null column
Next
Now, set Value Member to
Me.ComboBox1.ValueMember = "Year"
Also, needs more research, you may can do this:
"SELECT *, FunctionTOConvertToDate(YTD) as year FROM yeartbl"

find time in hour from two datetime in asp.net mvc using linq

I m working on timesheet module.What my problem is i have table with following fields
work_start_time Datetime
work_end_time Datetime
and other field
now my requirement is i have to calculate total hour submitted till date.
field are save like this format dd/mm/yy 10:00 AM
please anybody help me how to find total hour from above two field and sum total hour using LINQ.
any help would be appreciated.
int totalhours = (from d in db.timesheetWorkingHours
.Where(d=>d.WStartTime.HasValue && d.WEndTime.HasValue)
select (d.WStartTime - d.WEndTime).Hours).FirstOrDefault();
its gives error
'System.Nullable<System.TimeSpan>' does not contain a definition for 'Hours' and no extension method 'Hours' accepting a first argument of type 'System.Nullable<System.TimeSpan>' could be found (are you missing a using directive or an assembly reference?)
Here is the pseudo code. This may help. in your scenario timesheets object you would be getting from the db.
IList<Timesheet> timesheets = new List<Timesheet>();
timesheets.Add(new Timesheet
{
work_start_time = new DateTime(2012, 11, 5, 10, 0, 0),
work_end_time = new DateTime(2012, 11, 5, 19, 0, 0)
});
timesheets.Add(new Timesheet
{
work_start_time = new DateTime(2012, 11, 5, 10, 0, 0),
work_end_time = new DateTime(2012, 11, 5, 19, 0, 0)
});
int totalhours = 0;
timesheets.ToList().ForEach(
p => { TimeSpan ts = p.work_end_time - p.work_start_time; totalhours += ts.Hours; }
);
Console.WriteLine(totalhours.ToString());
Console.ReadKey();

asp.net mvc: SQLDateTimeOverflow problem with Entity Framework

I'm trying to update an object, and getting:
SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.
There are two fields in the object that are DateTime objects, and they're set with:
obj.created_date = DateTime.Now;
obj.modified_date = DateTime.Now;
Except that everything looks reasonable when I view the object:
>? obj.created_date
{1/13/2010 4:02:47 PM}
Date: {1/13/2010 12:00:00 AM}
Day: 13
DayOfWeek: Wednesday
DayOfYear: 13
Hour: 16
Kind: Unspecified
Millisecond: 817
Minute: 2
Month: 1
Second: 47
Ticks: 633989953678170000
TimeOfDay: {16:02:47.8170000}
Year: 2010
>? obj.modified_date
{1/19/2010 12:20:50 PM}
Date: {1/19/2010 12:00:00 AM}
Day: 19
DayOfWeek: Tuesday
DayOfYear: 19
Hour: 12
Kind: Local
Millisecond: 333
Minute: 20
Month: 1
Second: 50
Ticks: 633995004503331818
TimeOfDay: {12:20:50.3331818}
Year: 2010
Any idea what's going on? Even reading the fields from the database, then trying to save them back without changes, causes the error. Other classes have no problems, and I can't see any differences that would account for this error.
I've added
<globalization culture="en-US" />
to my web.config, with no changes.
These problems are on my local dev
OK, here's what was happening. marc_s had the right idea.
There's was binder handling the updates:
TryUpdateModel(editItem, new string[] { "LookupTable", "TextField1", "TextField2" });
I removed the "LookupTable" from the call to TryUpdateModel, and instead handle the value in the binder with:
var fkid = Convert.ToInt32(controllerContext.HttpContext.Request.Form["LookupTable.ID"]);
EntityKey ek = new EntityKey("entities.LookupTable", "ID", fkid);
obj.LookupTableReference.EntityKey = ek;
Apparently, the EF was trying to update the LookupTable value for some reason.

Resources