How could I parse this date value transferred from model to javascript? - javascript

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

Related

APPS SCRIPT DATE FORMAT ISSUE

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');

How to parse a Javascript datetime to C# DateTime parameter

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.

How to use moment.js properly?

I have made a sample page which contains this:
var date = '2015-04-03';
var formate = 'LLLL';
var result = moment(date).format(format);
console.log(result);
<script src="https://cdn.jsdelivr.net/momentjs/2.13.0/moment.min.js"></script>
And as you can see I have included the moment.js file in order to retrieve data from it and displaying a valid date in the browser. (Here is the link to moment.js that I have inlcuded: link)
But I don't know why it does not work at all! Can u guys help how to use moment.js in proper way ?! Thanks...
You wrote "formate" instead of "format".
Appart from this, you code works fine.
var date = '2015-04-03';
var format = 'LLLL';
var result = moment(date).format(format);
document.write(result);
<script src="https://cdn.jsdelivr.net/momentjs/2.13.0/moment.min.js"></script>

How can I get date and time separately using jQuery?

In my table date is listed as "2015-07-31 06:02:20". How can I get date and time separately using jQuery?
I used some code but it shows some errors.
var timestamp = new Date(data.responsecusthistory.created_at);
var date = timestamp.toDateString();
Error: Invalid Date
var date = data.responsecusthistory.created_at.split(" ")[0];
var time = data.responsecusthistory.created_at.split(" ")[1];
If you want to have a time string (i.e. HH:MM:SS), try e.g. this:
var timestampTime = timestamp.toTimeString().split(' ')[0];
Anyway, there's no obvious reason why you get the "Error: Invalid Date", as long as
data.responsecusthistory.created_at
is a correct value, such as "2015-07-31 06:02:20". Please consider double-checking this variable.
Try this:
dateString= "2015-07-31 06:02:20";
temp = new Date(dateString);
dateStr= $.datepicker.formatDate('mm/dd/yy', temp );
for getting different formats check the link https://github.com/phstc/jquery-dateFormat.
Using different formats we will get different date, time etc in which ever forms we need it.

Deserializing JSON dates timezone-less

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 .

Categories

Resources