Changing the Date format from Javascript to ASP.NET

I need to convert a date format from JavaScript to ASP.NET date format.

2012-09-10 12:00PM to /Date(1347442050050-0700)/

The reason for the conversion is because I am sending it back to the server. I extracted the ASP.NET format from the server request, then used moment.js to convert it to a JavaScript date:

moment("/Date(1347442050050-0700)/").format("YYYY-MM-DD hh:mmA");

Is there an efficient method to achieve this?

Answer №1

I have found the solution I was looking for. If you notice any issues, please feel free to leave a comment.

let startTime = moment("2015-03-25 09:30AM").valueOf();
let timeZone = moment("2015-03-25 09:30AM").format("ZZ");

let combinedTime = "/Date("+startTime + timeZone + ")/";

alert( combinedTime ); // returns /Date(1427281800000+0900)/

let formattedTime = moment(combinedTime).format("YYYY-MM-DD hh:mmA");

alert( formattedTime );​ // returns 2015-03-25 09:30AM

Answer №2

By integrating the function into the moment prototype, you can enhance its portability.

http://jsfiddle.net/timrwood/qe8pk/

moment.fn.toASP = function () {
    return '/Date(' + (+this) + this.format('ZZ') + ')';
}

Answer №3

In case you need to transmit a date to an ASP.NET ASMX web service that has an RPC method accepting a DateTime object, this resource could prove useful:

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Modifying Bracket Shell Name leads to WebSocket Connection Failure

I have been working on developing an application using the Brackets shell. Specifically, I am in the process of creating a customized code editor for a project rather than starting from scratch by modifying Brackets. Up until now, I have managed to addres ...

What is the method for updating the content within a div element?

I am trying to add multiple elements to my content, including an h1 element, an input field, and some buttons. I have written a function to create the h1 and input elements, but when trying to add them to my content div, I encountered an error (TypeError ...

Tips for triggering onclick before changing to a new page?

Currently, I am developing a React application and have encountered the following situation: <a href={link} variant="primary" onClick={this.onClickDoSomething}> here. </a> My goal is for the onClick event to trig ...

Establishing MQTT Connection in Ionic 3

I am facing a challenge with implementing Publish-Subscribe methods in my Ionic 3 application. After consulting the information on this page, I attempted to link MQTT with my Ionic 3 application. Can anyone guide me on how to successfully connect MQTT wi ...

Tally up the values of selected checkboxes

  I'm in search of a method to tally up data-values associated with checkboxes. In the example provided below, selecting locations from the checkboxes results in the calculation of primary values, which are displayed in the green box. I also need to ...

What is the process for embedding a Vue app within another Vue app?

I have been tasked with creating a widget that will be integrated into various customers' websites. Think about something like this: https://i.sstatic.net/3uKwL.png There are 2 constraints: The use of iframes is not allowed The customers' we ...

Is there a way to display a success message once the button has been activated?

<template> <div> <div class="form-group"> <label for="name">Name</label> <input type="text" class="form-control" v-model="firstName" placeholder="Enter ...

Having trouble receiving JSON/JSONP correctly

I've been exploring the world of APIs and I'm facing a roadblock right at the beginning. My goal is to extract data from the Fever Public API in order to retrieve a list of subscribed RSS feeds. The API appears to be straightforward - check it ou ...

Should information be retrieved from the server or the client side?

Allow me to clarify the situation at hand. I am currently developing a website where users can store movie information, sourced from a third-party API known as The Movie Database. At this juncture, my main concern pertains to optimizing performance throu ...

Utilizing Javascript to extract data from a JSON file

I have a task involving importing a nested JSON file and the challenge is to extract specific data from the file and display it in a table format. Any tips on efficiently navigating through the different levels of the JSON file to access only the innermos ...

Converting RGBA to Hex Color Code with Javascript: A Step-by-Step Guide

I attempted to change the rgba color representation to hexadecimal, but I encountered difficulty in converting the opacity value while successfully converting the remaining colors. Here is the snippet of my code: var colorcode = "rgba(0, 0, 0, 0.74)"; ...

Is there a way to make my JavaScript code function within an ASP.NET CSHTML file?

I am working on a web project where I have an HTML input for uploading a picture to my FTP server. I want to use JavaScript to detect changes on my upload button. This is the code in my CSHTML file: @using (Html.BeginForm("UploadFile", "Upload", For ...

The problem with the first item title in the Jquery slider is causing

I've been working on setting up a Jquery slider (caroufredsel) in which I want certain elements to be displayed above the slider itself, outside of the wrapper. Everything is working fine except for the first slide! After spending several days trying ...

Arrange a collection of objects in a JavaScript array based on a property value within each object

Hey there, I have an array of objects that need to be sorted (either DESC or ASC) by a specific property in each object. Below is the data: obj1 = new Object; obj1.date = 1307010000; obj2 = new Object; obj2.date = 1306923600; obj3 = new Object; obj3.d ...

Reinitializing AngularJS form controls

Check out this codepen example: http://codepen.io/anon/pen/EjKqbW I'm trying to reset the form elements when the user clicks on "Reset". The div elements f1,f2,f3 should be hidden and the searchText box cleared. However, the reset button is not trig ...

Obtain the HTML5 data attribute from an event directly in the code

I am attempting to access the data attribute in the following manner (my doctype is html5): <a href="#my-link" data-zoom="15" onclick="alert(this.data-zoom); return false;">link</a> However, I am encountering an e ...

Using $watchGroup to monitor changes in an array and automatically updating it

Issue: I am facing a challenge where I want to pass an array of variables into $watchGroup and iterate through the array to update the values of each element. However, the current approach I am using does not seem to be effective: $scope.secondsElapsed = ...

Is the sub-document _id generated by Mongoose unique?

Consider the mongoose schema below: mongoose.Schema({ world: String, color: [{ name: String }] }); This schema generates documents with sub-documents that have _id fields. { _id: 'a9ec8475bf0d285e10ca8d42' world: 'matrix', ...

Find the elements of <a> tags specifically without the attribute href

Currently, I am extracting the class of all <a> elements in an HTML document of a webpage using VB.net from a WinForm: Dim htmlLinks As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("a") For Each link As HtmlElement In htmlLi ...

Is it possible for me to transition my asp.net application to a cloud-based platform?

Our organization is contemplating a transition to cloud computing. We are concerned about whether we can still meet our current needs (listed below). Our goal is to have the ability to easily expand in the future without incurring high expenses. We have ...