However bash I format a Microsoft JSON day?

However bash I format a Microsoft JSON day?

I'm taking my archetypal ace astatine Ajax with jQuery. I'm getting my information onto my leaf, however I'm having any problem with the JSON information that is returned for Day information varieties. Fundamentally, I'm getting a drawstring backmost that appears to be like similar this:

/Date(1224043200000)/

From person wholly fresh to JSON - However bash I format this to a abbreviated day format? Ought to this beryllium dealt with location successful the jQuery codification? I've tried the jQuery.UI.datepicker plugin utilizing $.datepicker.formatDate() with out immoderate occurrence.

FYI: Present's the resolution I got here ahead with utilizing a operation of the solutions present:

function getMismatch(id) { $.getJSON("Main.aspx?Callback=GetMismatch", { MismatchId: id }, function (result) { $("#AuthMerchId").text(result.AuthorizationMerchantId); $("#SttlMerchId").text(result.SettlementMerchantId); $("#CreateDate").text(formatJSONDate(Date(result.AppendDts))); $("#ExpireDate").text(formatJSONDate(Date(result.ExpiresDts))); $("#LastUpdate").text(formatJSONDate(Date(result.LastUpdateDts))); $("#LastUpdatedBy").text(result.LastUpdateNt); $("#ProcessIn").text(result.ProcessIn); } ); return false;}function formatJSONDate(jsonDate) { var newDate = dateFormat(jsonDate, "mm/dd/yyyy"); return newDate;}

This resolution acquired my entity from the callback technique and displayed the dates connected the leaf decently utilizing the day format room.


eval() is not essential. This volition activity good:

var date = new Date(parseInt(jsonDate.substr(6)));

The substr() relation takes retired the /Date( portion, and the parseInt() relation will get the integer and ignores the )/ astatine the extremity. The ensuing figure is handed into the Date constructor.


I person deliberately near retired the radix (the 2nd statement to parseInt); seat my remark beneath.

Besides, I wholly hold with Rory's remark: ISO-8601 dates are most well-liked complete this aged format - truthful this format mostly shouldn't beryllium utilized for fresh improvement.

For ISO-8601 formatted JSON dates, conscionable walk the drawstring into the Date constructor:

var date = new Date(jsonDate); //no ugly parsing needed; full timezone support

You tin usage this to acquire a day from JSON:

var date = eval(jsonDate.replace(/\/Date\((\d+)\)\//gi, "new Date($1)"));

And past you tin usage a JavaScript Day Format book (1.2 KB once minified and gzipped) to show it arsenic you privation.


Running with dates successful JSON, particularly once interacting with Microsoft applied sciences similar ASP.Nett and jQuery, tin generally awareness similar navigating a minefield. The default JSON day format utilized by ASP.Nett, frequently referred to arsenic the Microsoft JSON day format, presents these dates arsenic a figure representing the milliseconds since the Unix epoch, wrapped successful a circumstantial drawstring. This tin beryllium tough to grip straight successful JavaScript. Knowing however to decently format these dates for show oregon additional processing is important for immoderate developer running successful this situation. Successful this article, we volition research antithetic approaches and strategies to efficaciously format Microsoft JSON dates utilizing jQuery and JavaScript.

Decoding and Formatting Microsoft JSON Dates

The Microsoft JSON day format, sometimes represented arsenic \/Day(1198908717056)\/, is a normal utilized by ASP.Nett to serialize dates into JSON. This format encodes the day arsenic the figure of milliseconds since the Unix epoch (January 1, 1970) and wraps it inside the \/Day() wrapper. Piece this format ensures that day accusation is preserved throughout serialization and deserialization, it's not straight quality-readable oregon easy processed by JavaScript's autochthonal day capabilities. So, builders essential person this format into a modular JavaScript Day entity to execute operations similar formatting and show.

Parsing the Microsoft JSON Day Drawstring

The archetypal measure successful formatting a Microsoft JSON day is to parse the day drawstring and extract the millisecond worth. This tin beryllium achieved utilizing JavaScript's drawstring manipulation capabilities. The end is to isolate the numerical worth inside the \/Day() wrapper. Erstwhile extracted, this worth tin beryllium utilized to make a JavaScript Day entity. This entity offers a broad scope of strategies for formatting and manipulating the day. Last creating the JavaScript Day entity, it turns into overmuch simpler to format the day into a much readable format utilizing constructed-successful JavaScript strategies oregon outer libraries.

 function parseMicrosoftJsonDate(dateString) { var milliseconds = parseInt(dateString.replace(/[^0-9]/g, '')); return new Date(milliseconds); } 

The codification snippet supra demonstrates however to extract the milliseconds from the Microsoft JSON day format. It makes use of a daily look to distance immoderate non-numeric characters from the day drawstring. Past, it parses the remaining drawstring arsenic an integer, representing the milliseconds since the Unix epoch. Eventually, it creates a fresh Day entity utilizing the parsed milliseconds. This Day entity tin past beryllium utilized for additional formatting and show.

Preview an cooperation earlier it is uploaded

Strategies for Formatting Dates successful jQuery

Last efficiently parsing the Microsoft JSON day, the adjacent project is to format it into a person-affable cooperation. jQuery, on with its ecosystem of plugins, offers respective choices for formatting dates. 1 communal attack is to usage the toLocaleDateString() methodology, which codecs the day in accordance to the locale settings of the person's browser. Alternatively, libraries similar Minute.js oregon Day-fns message much precocious formatting capabilities, permitting builders to specify customized day codecs. These libraries supply a broad scope of choices for displaying dates successful assorted codecs, specified arsenic "YYYY-MM-DD", "MM/DD/YYYY", oregon "DD MMM YYYY". Utilizing these instruments, you tin guarantee that dates are displayed successful a format that is some readable and accordant crossed antithetic browsers and gadgets. The prime of formatting methodology relies upon connected the circumstantial necessities of the exertion and the desired flat of customization.

Present's an illustration of utilizing JavaScript's toLocaleDateString() methodology:

 var date = parseMicrosoftJsonDate(jsonDate); var formattedDate = date.toLocaleDateString(); // e.g., "12/31/2024" 

For much power complete the day format, see utilizing Minute.js oregon Day-fns:

 // Using Moment.js var date = parseMicrosoftJsonDate(jsonDate); var formattedDate = moment(date).format('YYYY-MM-DD'); // e.g., "2024-12-31" // Using Date-fns var date = parseMicrosoftJsonDate(jsonDate); var formattedDate = format(date, 'yyyy-MM-dd'); // e.g., "2024-12-31" 

Beneath is a examination array displaying antithetic day formatting choices:

Methodology Statement Illustration
toLocaleDateString() Codecs the day in accordance to the locale settings. date.toLocaleDateString()
Minute.js Offers extended formatting choices with customized patterns. moment(date).format('YYYY-MM-DD')
Day-fns A light-weight alternate to Minute.js with akin formatting capabilities. format(date, 'yyyy-MM-dd')
"Selecting the correct day formatting room oregon methodology relies upon connected the complexity of your formatting wants and the dimension of your task. Libraries similar Minute.js and Day-fns message almighty options however whitethorn adhd to your task's bundle dimension."
  • Usage toLocaleDateString() for elemental, locale-alert formatting.
  • See Minute.js oregon Day-fns for analyzable, customized formatting wants.
  • Ever trial day formatting crossed antithetic browsers and locales.

Applicable Examples of Formatting Microsoft Dates

Fto's see a applicable illustration wherever you're retrieving JSON information from an ASP.Nett Internet API endpoint utilizing jQuery's $.ajax() relation. The JSON consequence comprises a day successful the Microsoft JSON format. You demand to show this day successful a person-affable format connected your webpage. The pursuing codification demonstrates however to accomplish this. Archetypal, the $.ajax() relation retrieves the JSON information. Past, the parseMicrosoftJsonDate() relation converts the Microsoft JSON day drawstring to a JavaScript Day entity. Eventually, the toLocaleDateString() methodology codecs the day and shows it successful the desired HTML component. This illustration showcases a absolute workflow for dealing with Microsoft JSON dates successful a existent-planet script.

 $.ajax({ url: '/api/data', method: 'GET', success: function(response) { var jsonDate = response.MyDate; // Assume MyDate is in Microsoft JSON format var date = parseMicrosoftJsonDate(jsonDate); var formattedDate = date.toLocaleDateString(); $('dateDisplay').text(formattedDate); // Display the formatted date in an element with ID "dateDisplay" } }); 

Present is different illustration of formatting a Microsoft JSON day utilizing Minute.js:

 $.ajax({ url: '/api/data', method: 'GET', success: function(response) { var jsonDate = response.MyDate; var date = parseMicrosoftJsonDate(jsonDate); var formattedDate = moment(date).format('MMMM Do YYYY, h:mm:ss a'); // e.g., "December 31st 2024, 11:59:59 pm" $('dateDisplay').text(formattedDate); } }); 

Successful decision, formatting Microsoft JSON dates utilizing jQuery entails parsing the day drawstring and past formatting the ensuing JavaScript Day entity utilizing due strategies oregon libraries. By knowing these strategies, builders tin efficaciously grip dates acquired from ASP.Nett providers and show them successful a person-affable format. Retrieve to take the formatting methodology that champion fits your task's wants and to trial your implementation totally crossed antithetic browsers and locales. Appropriate day formatting is important for making certain a accordant and person-affable education successful your internet purposes. Larn much astir jQuery. Moreover, knowing however day and clip are represented connected the internet ensures amended information dealing with and person education. For additional speechmaking, cheque retired JavaScript Day Entity and Minute.js documentation.


Powershell Quickes #1 - How to use Get-Location?

Powershell Quickes #1 - How to use Get-Location? from Youtube.com

Previous Post Next Post

Formulario de contacto