How to Format Datetime to Web Utc Format

How can I format DateTime to web UTC format?

string foo = yourDateTime.ToUniversalTime()
.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fff'Z'");

How can I print DateTime in UTC format?

I think you are missunderstanding what the API is doing.

First thing to note is that both DateTimeStyles.AssumeUniversal and DateTimeStyles.AssumeLocalwill still return a DateTime where Kind = Local

> DateTime.Parse("1970-01-01 00:00:00", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal).Kind
=> Local
> DateTime.Parse("1970-01-01 00:00:00", CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal).Kind
=> Local

So no matter what we will get a local date. That means that the API most likely is there to make it possible to get a local time from a UTC date. Let's try if that's correct.

I'm in Sweden so we are UTC + 1 during standard time. So if I use DateTimeStyles.AssumeUniversal and put in todays date I should get a local date being 01:00 today.

Running this in C# Interactive:

> DateTime.Parse("2018-03-03", .CultureInfo.InvariantCulture, .DateTimeStyles.AssumeUniversal)
=> [2018-03-03 01:00:00]

Meaning C# assumed that the string I inputed was in UTC and I wanted it in local so it "fixed" it for me.

Doing the same with AssumeLocal.

DateTime.Parse("2018-03-03", .CultureInfo.InvariantCulture, .DateTimeStyles.AssumeLocal)
=> [2018-03-03 00:00:00]

As expected we now treated the input as a local string and got the same value.

To get the date as UTC you can specify the kind

DateTime.SpecifyKind(DateTime.Parse("2018-03-03", CultureInfo.InvariantCulture), DateTimeKind.Utc).ToString("o")
=> "2018-03-03T00:00:00.0000000Z"

How to store DateTime at UTC format with Java 8?

The problem with LocalDateTime here is that it simply doesn't store an offset or a time zone, which means you cannot format it to a String that contains a Z for UTC respectively an offset of +00:00.

I would use a ZonedDateTime which will have an offset and a zone id and format is using a specific DateTimeFormatter (don't exactly know how your annotation will handle this).

Here's a small plain Java example:

public static void main(String[] args) {
// get an example time (the current moment) in UTC
ZonedDateTime now = ZonedDateTime.now(ZoneOffset.UTC);
// print its toString (implicitly)
System.out.println(now);
// format it using a built-in formatter
System.out.println(now.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));
// or define a formatter yourself and print the ZonedDateTime using that
System.out.println(now.format(DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ssz")));
}

The output of this small example was (some seconds ago):

2020-10-01T15:06:16.705916600Z
2020-10-01T15:06:16.7059166Z
2020-10-01T15:06:16Z

I think you can use such a pattern in the @JSONFormat annotation. Not entirely sure, but cannot look it up now.

How to convert a Date to UTC?

The toISOString() method returns a string in simplified extended ISO
format (ISO 8601), which is always 24 or 27 characters long
(YYYY-MM-DDTHH:mm:ss.sssZ or ±YYYYYY-MM-DDTHH:mm:ss.sssZ,
respectively). The timezone is always zero UTC offset, as denoted by
the suffix "Z".

Source: MDN web docs

The format you need is created with the .toISOString() method. For older browsers (ie8 and under), which don't natively support this method, the shim can be found here:

This will give you the ability to do what you need:

var isoDateString = new Date().toISOString();
console.log(isoDateString);

DateTime field does not get converted to UTC format when passed to web service request

Ok , Finally solved this.Link Force XmlSerializer to serialize DateTime as 'YYYY-MM-DD hh:mm:ss' was very useful. I added a string attribute similar to the one described in the link above and that seems to have fixed the issue.

/// <remarks/>
[System.Xml.Serialization.XmlIgnore]
public System.DateTime date
{
get
{
return this.dateField;
}
set
{
this.dateField = value;
}
}

/// <remarks/>

[System.Xml.Serialization.XmlElementAttribute("date", Order = 0)]
public System.String somedate
{
get { return this.date.ToString("yyyy'-'MM'-'dd'Z'"); }
set { this.date = System.DateTime.Parse(value); }

}

However, modifying generated proxy is definitely not the preferred way.

Converting new Date() to UTC format in typescript, angular

personModel: PersonDetail;    

const activationDate = new Date();

this.personModel.activationDate = new Date(activationDate.getUTCFullYear(),
activationDate.getUTCMonth(),
activationDate.getUTCDate(),
activationDate.getUTCHours(),
activationDate.getUTCMinutes(),
activationDate.getUTCSeconds()
);

OR
Can use a separate method to get UTCDate

const activationDate = this.getNowUTC();    

private getNowUTC() {
const now = new Date();
return new Date(now.getTime() + (now.getTimezoneOffset() * 60000));
}

A good article on converting DateTime

How to format a UTC date as a `YYYY-MM-DD hh:mm:ss` string using NodeJS?

If you're using Node.js, you're sure to have EcmaScript 5, and so Date has a toISOString method. You're asking for a slight modification of ISO8601:

new Date().toISOString()
> '2012-11-04T14:51:06.157Z'

So just cut a few things out, and you're set:

new Date().toISOString().
replace(/T/, ' '). // replace T with a space
replace(/\..+/, '') // delete the dot and everything after
> '2012-11-04 14:55:45'

Or, in one line: new Date().toISOString().replace(/T/, ' ').replace(/\..+/, '')

ISO8601 is necessarily UTC (also indicated by the trailing Z on the first result), so you get UTC by default (always a good thing).

Convert UTC date time to local date time

Append 'UTC' to the string before converting it to a date in javascript:

var date = new Date('6/29/2011 4:52:48 PM UTC');
date.toString() // "Wed Jun 29 2011 09:52:48 GMT-0700 (PDT)"

Javascript | Get Current UTC DateTime In DateTime Format - Not String

.toISOString() is what you want, not .toUTCString()

You already have the Javascript internal DateTime format in the variable dateTime_now. So I think you do want a string output, but not the string Sun, 05 Dec 2021 06:11:15 GMT, because it contains useless strings like Sun and useful, but non-numerical strings like Dec. I am guessing you do want a string output, but containing digits and separators and no words.

var dateTime_now = new Date();
var dateTime_now_utc_str = dateTime_now.toISOString();
console.log(dateTime_now_utc_str);

// Result: 2021-12-05T06:10:54.299Z

Display and Formatting UTC Time On Static Website in Real-Time

new Date().toISOString().slice(11, 19) + ' UTC'

A nice and intentional property of the ISO datetime format is that most of the parts are always the same length.

Note that truncating, not rounding, is the correct behavior.



Related Topics



Leave a reply



Submit