I have a problem that is breaking my head since yesterday and don't know how to deal with it.
I have a date field in my database that contains the following value:
Then my application get the value and send that to my web form. (the value still the same :) thats fine!!
In client side I put a break with a javascript alert to see the value that is comming from JSON (the value still the same :) thats fine!!
The code in client side for the alert is this:
// transaction json model
var jsonTransaction = #(Html.Raw(Json.Encode(this.Model.Transaction)));
alert(new Date(parseInt(jsonTransaction.Date.substr(6))));
Now when I send back the value to the server this is what I get
And finally after deserialization of the JSON my date time is wrong!! instead of Day 7 its now Day 8???????
This is the code for deserializing:
public JsonResult SaveBill(string jsonTransaction, string jsonTranDetails)
{
_appState = this.AppState;
JsonResult returnVal = returnVal = Json(
new { Success = true }
);
var transaction = JsonConvert.DeserializeObject<BillTransaction>(jsonTransaction, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore });
Any clue on how to solve this issue with dates, I should get the same date because I didn't change anything. Hope someone can guide me for a solution.
Thanks in advance.
Java script use universal time when it parse the date as currentdate = new Date(123232)
so when you send date to client convert it to ISO date such as
make sure the date is in UTC before convering it to strong .
return String.Format("{0:yyyy-MM-ddTHH:mm:ss.fffZ}", dt);
bty I already created JSON Converted to override any date serialization to client side to use the above function .
Related
I'm trying to send a table from google sheets to gmail using apps script, i have multiple dates and i want to send in this format "dd-mm-yy", but the problem is that i need to use utilites.formatdate which coverts the cell into string, so when i try to send the email it diplays that the parameters dont match the method, i think this is because in some cells date could be not fullfilled, so theres nothing written in the cell, because of this error i cant send the email, if i write dates in the cells it works but there are cases when they are not fullfilled as i said, how can i solved this problem and send the email no matter if all the dates are filled or not?
var fecha = registro.getRange("c16").getValue();
var fechaF = Utilities.formatDate(fecha, "GMT","dd-MM-YY");
This is the way i tried to change the format, and if there is someting written works but
var fecha2 = registro.getRange("e16").getValue();
var fecha2F = Utilities.formatDate(fecha2, "GMT","dd-MM-YY");
In the second there is nothing so it displays the error
Hope you can help me guys
I'm learning so all kind of advices you give to me are welcome
Try this:
var fecha = new Date(registro.getRange("c16").getValue());
var fechaF = Utilities.formatDate(fecha, "GMT","dd-MM-YY");
Try to initiate Date by adding new DATE() to use the converted string as DATE type and after that you can format it as you want
so your code will be
var fecha = new DATE(registro.getRange("c16").getValue());
EDIT
var fechaF = Utilities.formatDate(fecha, "GMT","dd-mm-yyyy").setNumberFormat('dd-mm-yyy');
According to my previous question here: How to send a JSON object using GET method, I could retrieve my values because the object is no longer null. My object contains one DateTime property called CreatedOn. I am sending the value from javascript as new Date(). Before to answer that I can get the DateTime.Now() from code-behind, there is a purpose to send the date from HTML.
Then, in debug mode when I arrive to the controller method, my CreatedOn property is always DateTime.MinValue = 01/01/0001 12:00:00 AM
I changed my javascript value to this format "yyyyMMddT000000" because I thought that this would be parsed automatically but I didn't have any success.
How can I do to send the value that can be parsed by the web api2 controller automatically?
<script>
$("#btnTest").on("click", function () {
var searchCriteria = {};
searchCriteria.ID = 0;
searchCriteria.Name = "";
//1. First tried option
//searchCriteria.CreatedOn = new Date();
//2. Second tried option. Test
searchCriteria.CreatedOn = "20170324T000000";
var url = "http://localhost:8080/api/products"
$.getJSON(url, searchCriteria).done(processResponse);
});
function processResponse(response){
}
</script>
I got it. Hope this can help others.
searchCriteria.CreatedOn = new Date().toISOString();
This will be parsed automatically.
Cheers.
I'm using MomentJS v2.8.4, and I'm trying to get formatted date like "31/12/2015"
myDate.format('DD/MM/YYYY') works fine until I set some "less English :)" localisation, e.g. Arabic. Then I get something like this ١٠/٠١/٢٠١٥, which is nice for the user, not so nice for API.
From MomentJS source code
format : function (inputString) {
var output = formatMoment(this, inputString || moment.defaultFormat);
// here I get correct "31/12/2015" format
return this.localeData().postformat(output); // this will return localized version
},
formatMoment function is not publicly exported...
Can you please suggest correct solution for this?
You could save the current locale() setting in a variable (i.e save the user's setting) and then explicitly set the locale value so that you can get your date format correct for your API call, then set the locale value back to the saved value.
Something like:
var userLocaleSetting = moment.locale();
moment.locale('en');
var myFormattedDate = myDate.format('DD/MM/YYYY');
moment.locale( userLocaleSetting );
One solution may be to return an object with the api and user formatted date.
format : function (inputString) {
var api = formatMoment(this, inputString || moment.defaultFormat);
// here I get correct "31/12/2015" format
var user = this.localeData().postformat(api); // this will return localized version
return {api: api, user: user};
},
var time =<%:Html.Raw(Json.Encode(Model.AvailableDates))%>;
var parsed = time[0];
alert( parsed );
The pop up shows: "/Date(1174021200000)/". It is not a instance of date. I tried .toString("mm/dd/yy"), Date.parse(parsed), new Date(parsed). Unfortunately, none of these works. I dont wanna let my controller return a formatted value. Is there a way I can parse it on client side? thank you. By the way AvailableDates is a list of datetime in c#.
you can parse it like this:
var date = new Date(<% Model.AvailableDates %>);
Try use moment.js
var time =<%:Html.Raw(Json.Encode(Model.AvailableDates))%>;
var parsed = moment(time);
alert( parsed );
See an example here in jsfiddle
I have a code that runs to select a date from calender, but I want to set the current_date to that of the server in the mentioned calender. And with a functionality that compares client_date with server_date.
That's possible. As suggested elsewhere, you should convert the dates to strings for two reasons:
Easier to read/understand when you search for a bug
The "current time" is always different between client and server
To send the current date to the client, you can use a hidden form field or an AJAX request or a web service or REST or JSON or JSONP or ... well, there are too many methods to do it.
To compare dates, convert the string to a Date object and then use the usual methods to compare them (compareTo() in Java and <,==,> in JavaScript).
You can do it by using the following way:
In Java
public class YourJavaClass{
public static String getServerDate()
{
Calendar cal = Calendar.getInstance();
return cal.get(Calendar.YEAR) + "-" + cal.get(Calendar.MONTH) + "-" + cal.get(Calendar.DATE);
}
}
In jsp
$('input.datepicker').datepicker(
{
minDate : new Date(<%out.print(YourJavaClass.getServerDate());%>)
});
The easiest way is to compare both dates with conversation to milliseconds. Both languages provides methods for it.
Javascript:
<script type="">
var myDate = new Date();
var milliseconds = myDate.getTime();
</script>
Java (JSP):
<script type="text/javascript">
<%!
Date myJavaDate = new Date();
long myJavaMilliseconds = 0L;
myJavaMilliseconds = myJavaDate.getTime();
%>
var myJavaMillisondsInJs = <%= myJavaMilliseconds %>;
</script>
/* Put code here to compare between those dates */
If you want to compare the dateas server side you have to submit the javatime to the server via Ajax or a form.
One way of doing it is to pass the current date from your server script to the template, so when the pages renders, javascript can access it and make comparison. This has obvious issue - if the user will leave the page open till next day, the date will be out of date.
Other way is to use asynchronous call to your back-end and obtain the date from there.