Sitemap lastmod date in Javascript - javascript

I need to the date format that is shown in <lastmod></lastmod> tags in sitemap.xml. How can I achieve that?
Output like:
<lastmod>2016-01-21EEST18:01:18+03:00</lastmod>
Where 'EEST' is probably timezone offset.
I have seen this question and the answers in php:
Google Sitemap Date Format
but don't know how to achieve it in Javascript,
Thanks.
EDIT: the question is not duplicate, since I need the correct time format in JavaScript for this.

The lastmod tag uses YYYY-MM-DDThh:mmTZD format, where TZD is the time zone offset. The W3 date and time format gives you 3 options for how to do the TZD: (Z or +hh:mm or -hh:mm)
This means that in javascript you can just use
const date = new Date().toISOString();
and it'll look like 2017-11-15T11:18:17.266Z, which is correct for a sitemap lastmod.

Related

How to get this format moment().format('YYYY-MM-DD\THH:mm:ssZ') javascript in php?

I wish to achieve this same date format from javascript in php:
moment().format('YYYY-MM-DD\THH:mm:ssZ');
output: 2016-12-24T13:46:43-05:00
I'm trying to get the same result, but I've only gotten the following:
$date = new DateTime();
echo $date->format('Y-m-d\TH:i:s-ssZ');
output: 2022-09-22T14:42:28-28280
I don't know if the procedure is correct, but I want to get the same javascript date format but in PHP, what changes should I add to my code?
The procedure is correct, you just need different formatting tokens. The documentation for DateTime::format lists what you can use.
What you can use in this case is P - the documentation says:
Difference to Greenwich time (GMT) with colon between hours and minutes Example: +02:00
echo $date->format('Y-m-d\TH:i:sP');
Demo: https://3v4l.org/PjI5m
N.B. s is for seconds, it has nothing to do with the timezone offset, so I'm not really sure why you used that, especially when you'd already used it to display seconds a bit earlier in the same formatting string (thus making it clear that is has a different purpose). Keep in mind that PHP's date formatting tokens are not the same as the ones used by momentJS.

How to parse jqwidgets (jqx) date with format?

Basically I have date format which comes from server and which will be not known before and a date that was already formatted in this format, e.g. the format is "dd/MM/yyyy" and the date is "21/01/2018" (two strings). The format that uses jqx differs from the default JS one, as I can see.
What I need to do is to parse it to JavaScript date object. I couldn't find the answer in the official documentation.
Is there any easy was of parsing the date string if you know the date format, using JS, jqwidgets or momentjs?
Eventually, I've finished with inner jqx method that does that exact thing:
var parsedDate = $.jqx.dataFormat.parsedate(src, format);
Where format in this case is "dd/MM/yyyy" and src is "21/01/2018".
P.S. Thanks for the guy that posted anwer with the code that used $.jqx.dataFormat, that really helped me to find parsedate method.

Date format with Moment.js or classic JS, change places of day and month?

I get date value from my source in this format:
02.08.2016 / Day.Month.Year, it's correct.
but JavaScript understand this inverse, Month.Day.Year.
I tried some ways with Moment.js like:
moment('15.08.2016').format('MM.DD.YYYY');
My JsFiddle is: http://jsfiddle.net/PAc3j/389/
There are some results visible, see please output, variable dateThree: Invalid date
My question is, how can I do it with Moment.js or classic JavaScript?
You can specify the format of the input date string like this.
moment('15.08.2016', 'DD.MM.YYYY').format('MM.DD.YYYY');
http://momentjs.com/docs/#/parsing/string-format

Get the given date format (the string specifying the format) in javascript or momentjs

Given a datestring, how can I get the format string describing that datestring?
Put another way, how can I get the format string that Date() or MomentJS (might be different for each, that's fine) would use to parse that datestring if one didn't pass an explicit format to use?
So given '2016-01-01' it should output something like 'YYYY-MM-DD', for example.
(I am aware this is a simple question and may have an answer somewhere, but it is difficult to word concisely, so I could only find questions and answers about how to parse datestrings or how to display dates. None about how to output the format itself.)
Consolidating information from Matt Johnson's answer, some comments, and my own contribution.
With Moment.js (version 2.10.7+), you can use the Creation Data API. Something like this in Node.js:
moment('2016-01-01 00:00:00').creationData().format
outputs
'YYYY-MM-DD HH:mm:ss'
Just as any date parsing is, there is ambiguity about the format of many datestrings due to things such as locale (the order of months and days are switched between the US and Europe, for example). But the above method is sufficient for me.
You can't, without having additional information, such as the locale. For example, 01/12/16 could be Jan 12, 2016, December 1, 2016, or December 16, 2001.
Even when you know the locale, there are several places in the real world where more than one date format is used, depending on context.
See https://en.wikipedia.org/wiki/Date_format_by_country
However, if you are just trying to determine which one of multiple known formats was used to parse the input string, moment has an API for that called Creation Data. For example:
var m = moment("2016/06/10", ["YYYY-MM-DD", "MM/DD/YYYY"], true);
var f = m.creationData().format; // "MM/DD/YYYY"

How to parse date time in JavaScript

I have Date in this format mm/dd/yy example: 04/11/13
and time in the format HH:MM:SS example: 17:02:30
I have to parse above two values and put in a variable dateTime with following format
YYYY-MM-DDTHH:MM:SSS
2013-04-11T17:02:30.000
What is best way to do it in AngularJS or in Javascript. I also need to verify the user input and make sure it is a valid mm/dd/yy date and a valid HH:MM:SS time
I know there are tons of duplicate/similar questions but I couldn't find one which answers above, please let me know if you found one.
You don't need an external library to do this. See this doc link for the forms of date that JavaScript can process normally.
For a specific solution to your question:
var date = new Date("04/11/13" + " " + "17:02:30");
date.toISOString();
>> "2013-04-11T21:02:30.000Z"
See this MDN page for more info on the Date object.
The best way to do this in AngularJS is with a filter. You bind to the dateTime, and then filter it with the date filter.
<span>{{myDate | date:yyyy-MM-ddThh:mm:sss}}</span>
Or in your controller you say:
$filter('date')(date[, format])
Here is more info: http://docs.angularjs.org/api/ng.filter:date

Categories