WCF DataMember DateTime Serializing Format

I have a working WCF service which used JSON as its RequestFormat and ResponseFormat.

[ServiceContract] public interface IServiceJSON { [OperationContract] [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] MyClassA echo(MyClassA oMyObject); } [DataContract] public class MyClassA { [DataMember] public string message; [DataMember] public List<MyClassB> myList; public MyClassA() { myList = new List<MyClassB>(); } } [DataContract] public class MyClassB { [DataMember] public int myInt; [DataMember] public double myDouble; [DataMember] public bool myBool; [DataMember] public DateTime myDateTime; }

The myDateTime property of class MyClassB is of type DateTime. This is being serialized to the following format: "myDateTime":"/Date(1329919837509+0100)/"

The client I need to communicate with can not deal with this format. It requires it to be a more conventional format like for example: yyyy-MM-dd hh:mm:ss

Is it somehow possible to add this to the DataMember attribute? Like so:

[DataMember format = “yyyy-MM-dd hh:mm:ss”] public DateTime myDateTime;

Thanks in advance!

-------------Problems Reply------------

Why not just pass it as an already formatted string?

That is, don't pass the date in your DataContract as a date. Make that member a string instead, and format the string the way your client it wants it.

Here's an example of the already checked answer...

public class ProductExport
public Guid ExportID { get; set; }

[DataMember( EmitDefaultValue = false, Name = "updateStartDate" )]
public string UpdateStartDateStr
if( this.UpdateStartDate.HasValue )
return this.UpdateStartDate.Value.ToUniversalTime().ToString( "s", CultureInfo.InvariantCulture );
return null;
// should implement this...

// this property is not transformed to JSon. Basically hidden
public DateTime? UpdateStartDate { get; set; }

public ExportStatus Status { get; set; }

The class above defines two methods to handle the UpdateStartDate. One that contains the nullable DateTime property, and the other convert the DateTime? to a string for the JSon response from my service.

Category:json Views:5 Time:2012-02-22

Related post

  • Comparing signatures in Ink Serialized Format 2009-12-03

    I have a load of signatures I wish to compare (all in ISF - ink serialized format). How to compare these signatures? --------------Solutions------------- Well I ended up using a mperfect idea and analysing the signatures using speed, time, distance a

  • NSTimeInterval format for POST with JSON objective-c to DateTime .net format 2011-06-10

    I retrieve the NSTimeInterval as follows: NSTimeInterval milisecondedDate = ([[NSDate date] timeIntervalSince1970] * 1000); Which logs this: UNIX Timestamp: "1307710190993.865967" I only want the value before the dot to send with JSON like this: "/Da

  • C# ticks to datetime and format 2011-08-05

    How can I convert ticks to datetime and format them to "ss:fff"? My code: public partial class MainWindow : Window { private DispatcherTimer timer; private long _ticks = 0; public MainWindow() { InitializeComponent(); } private void Start(object send

  • WCF Guid DataMember not Serialized properly 2011-11-02

    I have a WCF service that passes back and forth the following DataContracts: [DataContract] public class RequestWrapper { [DataMember] public FooDataContract FooDataContract; } [DataContract] public class ResponseWrapper { [DataMember] public FooData

  • WCF DataMember Serializing questions 2009-11-13

    Ok, so I was part way through the long winded process of creating DTOs for sending my model over the wire and I don't feel like I'm going down the right route. My issue is that most of the entities in my model are not much more that DTOs anyway. I ba

  • WCF Ria DateTime http query serialization 2010-09-02

    I need to call a wcf ria domain service and I want to use filtering. I need to code the http url explicitly myself. I found the following example on the net here: https: //..../SnagAllPeeps?$where=(Age%3e35) This will call SnagAllPeeps such that all

  • Can I pass array using WCF DataMember 2011-12-10

    Assume my WCF interface declared like that: [ServiceContract] public interface IManagementConsole { [OperationContract] ConsoleData GetData(int strategyId); [DataContract] public class ConsoleData { ..... [DataMember] public int[] GetConnectionsStats

  • WCF: DataMember attribute on property vs. member 2009-02-17

    In wcf, what is the difference between applying the DataMember attribute on a property private int m_SomeValue; [DataMember] public int SomeValue { get {...} set {...} } instead of a member variable [DataMember] private int m_SomeValue; public int So

  • Where's the DateTime 'Z' format specifier? 2009-05-07

    [Update: Format specifiers are not the same thing as format strings; a format specifier is a piece of a custom format string, where a format string is 'stock' and doesn't provide customization. My problem is with specifiers not formats] I've been try

  • MSMQ thru WCF is swallowing badly formatted messages, no errors, no warnings 2010-10-01

    I have a service that responds to messages on an MSMQ, using WCF. When the message is well formatted, it works as normal, and we're happy. When the message is intended for a different service, and the action is different, the message stays on the que

  • How do I prevent WCF from auto-serializing byte-array as base-64 encoded string? 2010-10-25

    When invoking a service, my client code passes in data. One of the data members is a byte array, so WCF will automatically encode this into a base-64 string during serialization. The problem is, the data contract of the service is expecting a byte ar

  • When WCF DataMember Is Not Required 2011-11-22

    I have got below two public properties in my DataContract class. I want to use this in client side. But I don't want to return this through the service. Don't I need DataMember Attribute for MyDateString? [DataMember] public DateTime MyDate { get; se

  • WCF HttpWebRequest Json date format 2011-12-12

    I'm trying to call a WCF JSON service with HttpWebRequest http://localhost/WCFService/class/sessions?startdate=\\/Date({0})\\/&endDate=\\/Date({1})\\/ This is my call The value for the Dates is DateTime.Now.Ticks The trace tells me "String was no

  • How can I convert string to datetime with format specification in JavaScript? 2009-01-24

    How can I convert a string to a date time object in javascript by specifying a format string? I am looking for something like: var dateTime = convertToDateTime("23.11.2009 12:34:56", "dd.MM.yyyy HH:mm:ss"); --------------Solutions------------- I thin

  • .Net: DateTime String format 2009-03-03

    I'm trying to add a timestamp to a file. However the DateTime Format ToString() looks weird to me. Here is my code: Dim _timeStamp As String = Date.Now.ToString("dd_mm_yyyy") but the value looks like this: _timeStamp = "03_24_2009" I checked my PC an

  • Stumped on C# DateTime ToString() formatting problem 2009-04-30

    I am getting some junk data returned from a ToString() call on a DateTime object in C# and I'm afraid I'm stumped after poking around with it for a while. The function is supposed to format dates to be compliant with RFC 822 (as required by the RSS s

  • Trying to parse String to DateTime into format "02/01/2010" dd/MM/yyyy rather than "1/2/2010 MM/dd/yyyy 2010-02-03

    I am trying to convert a string into date time format. DateTime.Parse(tempfrmBankDetails.dgvBankDetails.SelectedRows[0].Cells["PaymentDate"].Value.ToString(),null); This is printed as an output but i want it in dd/MM/yyyy format. How should i parse i

  • Datetime string formatting and CultureInfo 2010-04-30

    I was suprised to see FxCop complaining that I wasn't specifying a CultureInfo in the following: string s = day.ToString("ddd"); Surely the format string param in ToString is completely independent of CultureInfo? Is there any way which CultureInfo c

  • Help with DateTime in format I need 2010-05-03

    I am working with DateTime, trying to get date in the format like this: 03.05.2010-04.05.23 that is: dd.MM.yyyy-HH.mm.ss I'm using DateTime.ParseExact to try to achieve this (maybe wrong) So far I have: var dateInFormat = DateTime.ParseExact(DateTime

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.211 (s). 11 q(s)