11-06-2015 12:44:30
My datetime has the above format however it isn't getting converted to Local Time it gives me the month as November instead.
var check = moment('#Model.Invoice.InvoiceDate').format('YYYY-MM-DD HH:mm:ss');
var localTime = moment.utc(check).toDate();
localTime = moment(localTime).format('YYYY-MM-DD HH:mm:ss');
console.log(localTime);
White saving i am using DateTime.UTCNow function of C# and while getting the data i am using the following code.
Code:
var formatDate = new Date('#Model.Invoice.InvoiceDate.ToLocalTime()');
console.log(formatDate);
formatDate = moment.utc(formatDate).toDate();
console.log(formatDate);
var dateTime = moment(formatDate).format('lll');
console.log(dateTime);
Example of what is happening:
var formatDate = new Date('Sat Jun 13 2015 13:00:11 GMT+0530 (India Standard Time)');
console.log(formatDate);
formatDate = moment.utc(formatDate).toDate();
console.log(formatDate);
var dateTime = moment(formatDate).format('lll');
console.log(dateTime);
You can print an UTC ISO 8601 date for moment with ToString("s") but it will lack the Z, so you need to add it yourself.
var localTime = moment('#String.concat(Model.Invoice.InvoiceDate.ToString("s"), "Z")').format('lll');
Or by adding the Z on client side :
var localTime = moment('#Model.Invoice.InvoiceDate.ToString("s")' + 'Z').format('lll');
Try this
var check = moment('11-06-2015 12:44:30', 'DD-MM-YYYY HH:mm:ss')
.format('YYYY-MM-DD HH:mm:ss');
or in your case this
var check = moment('#Model.Invoice.InvoiceDate', 'DD-MM-YYYY HH:mm:ss')
.format('YYYY-MM-DD HH:mm:ss');
instead of
var check = moment('#Model.Invoice.InvoiceDate')
.format('YYYY-MM-DD HH:mm:ss');
While giving moments a date string you need to specify which part is what.
Related
I have a log file with some timestamps
2020-12-03 08:30:00
2020-12-03 08:40:00
...
I know from the log provider's documentation that the timestamps are written in UTC (although not using ISO format)
Now I want to parse them with date-fns :
const toParse = "2020-12-03 08:40:00"
parse(toParse, 'yyyy-MM-dd HH:mm:ss', new Date()).toISOString()
And because the locale of my computer is in UTC+1 here is what I see:
> "2020-12-03T07:40:00Z"
expected:
> "2020-12-03T08:40:00Z".
Here is the hack I currently use to tell date-fns to parse as UTC :
const toParse = "2020-12-03 08:40:00"
parse(toParse + '+00', 'yyyy-MM-dd HH:mm:ss' + 'X', new Date()).toISOString()
And as expected,
> "2020-12-03T08:40:00Z".
Is there any proper way of doing this using date-fns? Looking for an equivalent to moment's moment.utc()
I don't know about "proper", but you can use zonedTimeToUtc to treat a timestamp as having any offset or timezone you like, including UTC, e.g.
// Setup
var {parse} = require('date-fns');
var {zonedTimeToUtc} = require('date-fns-tz');
// Parse an ISO 8601 timestamp recognised by date-fns
let loc = 'UTC';
let s1 = '2020-12-03 08:30:00';
let utcDate = zonedTimeToUtc(s1, loc);
// Show UTC ISO 8601 timestamp
console.log(utcDate.toISOString()); // "2020-12-03T08:30:00.000Z"
// Parse non–standard format yyyyMMdd
let s2 = '20210119';
let fIn = 'yyyyMMdd';
let d = zonedTimeToUtc(parse(s2, fIn, new Date()), loc);
console.log(d.toISOString()); // "2021-01-19T00:00:00.000Z"```
You can test it at npm.runkit.com/date-fns.
I think you are looking for parseJSON, which supports a number of formats (but does not let you specify the source format).
Converts a complete ISO date string in UTC time, the typical format for transmitting a date in JSON, to a JavaScript Date instance.
import { parseJSON } from 'date-fns';
const utcDate = parseJSON('2020-12-03 08:40:00');
// Thu Dec 03 2020 19:40:00 GMT+1100 (Australian Eastern Daylight Time)
Example of using parse and zonedTimeToUtc
it('should parse polish date', async () => {
expect.assertions(1)
const dateWithoutTime = '29 gru 2003'
const parsed = parse(dateWithoutTime, 'd LLL yyyy', new Date(), {
locale: pl,
})
const dateUTC = zonedTimeToUtc(parsed, 'UTC')
expect(dateUTC.toISOString()).toStrictEqual('2003-12-29T00:00:00.000Z')
})
I need to read date from js Date object as if it was in UTC, however the object contains timezone.
How do I create moment object as if it was in UTC when it has some Timezone data?
you can do this by using moment.utc().
moment([2011, 10, 8, 5]).format();
moment.utc([2011, 10, 8, 5]).format();
If you want your timezone completely ignored, you can use this code.
var firstDayStr = '29 January 2014';
var startAtTime = '10:01:02 AM';
var localFormat = 'YYYY-MM-DD[T]HH:mm:ss';
var m = moment(firstDayStr + ' ' + startAtTime).format(localFormat);
You can use the UTC mode of moment.js
UTC
By default, moment parses and displays in local time.
If you want to parse or display a moment in UTC, you can use moment.utc() instead of moment().
var date = new Date().toLocaleString("en-US", {timeZone: "America/New_York"})
var now = moment().format('MMM DD h:mm A');
var nowUTC = moment.utc().format('MMM DD h:mm A');
console.log(now);
console.log(nowUTC);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
I'd like to convert a UTC date string into the current users timezone and maintain the format of the date string.
For example, I have this code which works:
var data = '2017-04-24 12:06:37';
var date = new Date(data+' UTC');
console.log(date.toString()); // logs "Mon Apr 24 2017 08:06:37 GMT-0400 (Eastern Daylight Time)"
How do I get this to output the new date string in the exact same format dynamically?
You have to add UTC to the string before you convert it to a date.
To convert any date to a UTC string, you can use
var UTCstring = (new Date()).toUTCString();
See my snippet. It compares the five different methods.
var date = new Date('2017-04-24 12:06:37 PM UTC');
var local = date.toString();
var utc = date.toUTCString();
var iso = date.toISOString();
var dateString = date.toDateString();
console.log(date);
console.log(local);
console.log(utc);
console.log(iso);
console.log(dateString);
Solution I used with momentjs:
moment.utc(value).local().format('YYYY-MM-DD HH:mm:ss');
The date format i receive from JSON is like this :-
/Date(1412101800000)/
When i convert this into dateformat, i get 1 day minus.
I am using following code :-
var dateFormat = new Date(parseInt(obj['DATEOFJOINING'].substr(6))).toISOString().substr(0, 10);
dateFormat results in 2014-09-30
The Original Date which comes from Db is 2014-10-01
Why is this happening? How to get perfect date?
It's a timestamp.
new Date(parseInt('/Date(1412101800000)/'.substr(6)));
Just set a right timezone.
var dateFormat = new Date(parseInt('/Date(1412101800000)/'.substr(6)));
dateFormat.setTime( dateFormat.getTime() + dateFormat.getTimezoneOffset()*60*1000 );
Date in your timezone*: 30/09/2014 19:30:00
Date in Los Angeles*: 30/09/2014 11:30:00
Date in Berlin* :30/09/2014 19:30:00
Date in Beijing*: 01/10/2014 01:30:00
Date in New York* :30/09/2014 13:30:00
For example
var dateFormat = new Date(parseInt('/Date(1412101800000)/'.substr(6)));
dateFormat.setTime( dateFormat.getTime() + dateFormat.getTimezoneOffset()*(-10*100000));
Date {Wed Oct 01 2014 12:10:00 GMT+0100 (BST)}
As #madforstrength commented, it uses the offset from GMT.
To get the offset in minutes you can try:
var d = new Date()
var n = d.getTimezoneOffset();
and adjust for your local time.
You can see a reference here.
This is due to the clients timezone. Use Date.prototype.getTimezoneOffset to get the offset:
var date = new Date(1412101800000);
var gmtDate = new Date(date.valueOf() + date.getTimezoneOffset() * 60 * 1000);
I have a datestring, pulled from an external source, that looks like this:
9/25/2011 4:38:40 PM
That source in the the PDT timezone.
I'd like to create a UTC date out of that information, using date.js. I'm using this code to parse it at present:
var dateString = '9/25/2011 4:38:40 PM';
var d = Date.parseExact('9/25/2011 4:38:40 PM', 'M/d/yyyy H:m:s tt');
While this does load the date, it does so as if it were in my timezone. How can I tell date.js that the date I'm telling it is from a different time zone?
Use timezone format specifier...
var dateString = '9/25/2011 4:38:40 PM EST';
var d = Date.parseExact(dateString, 'M/d/yyyy H:m:s tt Z');
putting an e in a date format will signify the timezone. I haven't tested this, but:
Date.parseExact(dateString + " PDT", "M/d/yyyy H:m:s tt e")
Does not account for daylight savings time shifts (PST instead of PDT), but you get the gist.
Have you tried something like this?:
var dateString = '9/25/2011 4:38:40 PM';
var date = Date.parseExact(dateString, format);
var utc_date = new Date(date.getTime() - (date.getTimezoneOffset() * 60000))